2017-06-13 312 views
1

我使用Nginx作为Web服务器使用LetsEncrypt加密我的连接。 我的主页有多个子域,如www.domain.com,mail.domain.com等。 现在我想将所有“/.well-known”连接重定向到一个特殊的根文件夹。Nginx - 错误的重定向?

我想我有正确的配置,但结果不是我想要的。

这里我CONFIGS:

default.vhost

#============================================= 
#== Server 
#============================================== 

server { 

     #============================================= 
     #== General 
     #============================================= 

     # Port 
     listen 80; 

     # Server name 
     #server_name _; 

     #============================================= 
     #== Locations 
     #============================================= 

     location /.well-known { 
       #default_type "text/plain"; 
       root /var/wwww/LetsEncrypt; 
     } 

     location/{ 
       return 301 https://www.example.com$request_uri; 
     } 
} 


server { 

     #============================================= 
     #== General 
     #============================================= 

     # Port 
     listen 443 ssl; 

     # Server name 
     #server_name _; 

     #============================================= 
     #== Locations 
     #============================================= 

     location/{ 
       return 301 https://www.example.com$request_uri; 
     } 
} 

www.domain.com

#============================================= 
#== HTTP-Server 
#============================================= 

server { 

     #============================================= 
     #== General 
     #============================================= 

     # Port 
     listen 443 ssl default_server; 

     # Server name 
     server_name www.example.com; 

     # Root folder 
     root /var/www/www.example.com/web/; 

     # Order of index files 
     index index.php index.html index.htm; 

     #============================================= 
     #== Includes 
     #============================================= 

     # SSL configuration 
     include /etc/nginx/ssl.conf; 

     # PHP configuration 
     include /etc/nginx/php.conf; 

     #============================================= 
     #== Locations 
     #============================================= 

     location /.well-known/acme-challenge { 
       #default_type "text/plain"; 
       root /var/wwww/LetsEncrypt; 
     } 

     location/{ 
       try_files $uri $uri/ =404; 
     } 

     location ~ /\.ht { 
       deny all; 
     } 
} 

mail.domain.com

#============================================= 
#== HTTP-Server 
#============================================= 

server { 

     #============================================= 
     #== General 
     #============================================= 

     # Port 
     listen 443 ssl; 

     # Server name 
     server_name mail.example.com; 

     # Root folder 
     root /var/www/mail.example.com/web/; 

     # Order of index files 
     index index.php index.html index.htm; 

     #============================================= 
     #== Includes 
     #============================================= 

     # SSL configuration 
     include /etc/nginx/ssl.conf; 

     # PHP configuration 
     include /etc/nginx/php.conf; 

     #============================================= 
     #== Locations 
     #============================================= 

     location/{ 
       try_files $uri $uri/ =404; 
     } 

     location ~ /\.ht { 
       deny all; 
     } 
} 

那么,会发生什么...... mail.domain.com和www.domain.com的效果很好。 问题是众所周知的规则。 Nginx忽略它,我总是重定向,而不是从“/ var/www/LetsEncrypt”获取文件。

我认为default_server是问题,但在将它移动到“default.vhost”后,没有任何工作(总是404 ..什么也听起来有点奇怪...)。

我希望你们能帮助我:(

类方面, 安德烈亚斯

回答

0

首先你要使用nginx 'location begins with' modifier ^~将捕获与指定的目录开始的所有请求。

location ^~ /.well-known/acme-challenge { 

其次你有/var/wwww/LetsEncrypt(4 w's)而不是/var/www/LetsEncrypt

您的位置块应该是这样的:

location ^~ /.well-known/acme-challenge { 
    #default_type "text/plain"; 
    root /var/www/LetsEncrypt; 
} 

也知道alias与根之间的差异以root身份将URI附加到路径,如:

alias /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt 

root /var/www/LetsEncrypt; # Will serve files from /var/www/LetsEncrypt/.well-known/acme-challenge 

这可能会发觉你的如果你想提供一个特定的目录。

+0

好的,我会稍后再试。 感谢您的快速回复:) – Andreas

+0

所以,我已经测试过的变化,它的作品完美!只有“别名”,而不是“根”没有奏效,但它没有什么大问题。我会将你的答案设定为接受的答案! – Andreas