2017-06-13 95 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