2015-10-04 71 views
0

我已經改變了我的conf文件,以便在時域中的用戶類型,而WWW重定向到域以www:重定向過多 - 改變裸體網址 - > www.example.com

server_name example.com; 
return 301 $scheme://www.example.com$request_uri; 

我也希望我的HTTPS下的任何/用戶

我得到的錯誤也可能重定向,我哪裏錯了?

所以我必須:

server { 
    listen 80 default_server; 
    listen [::]:80 default_server ipv6only=on; 

    root /var/www/example.com/site; 
    index index.html index.htm; 

    # Make site accessible from http://localhost/ 
    server_name example.com; 
    return 301 $scheme://www.example.com$request_uri; 

    location/{ 
      # First attempt to serve request as file, then 
      # as directory, then fall back to displaying a 404. 
      try_files $uri $uri/ =404; 
      # Uncomment to enable naxsi on this location 
      # include /etc/nginx/naxsi.rules 
    } 
    location /user { 
      rewrite^https://$http_host$request_uri? permanent; 
    } 

}

對於443端口:

server { 
    listen 443; 
    server_name example.com; 
    return 301 $scheme://www.example.com$request_uri; 

    root /var/www/example.com/site; 
    index index.html index.htm; 

    ssl on; 
    ssl_certificate //path here 
    ssl_certificate_key //path here 

    location/{ 
      rewrite^http://$http_host$request_uri? permanent; 
    } 
    location /user { 
    } 

}

回答

0

隨着

listen 80 default_server; 

您告訴nginx,無論服務器名稱如何,此服務器塊都是所有http請求的默認服務器。

該指令

return 301 $scheme://www.example.com$request_uri; 

套nginx的重定向所有傳入流量,這個服務器塊www.example.com。重定向的流量再次碰到相同的服務器塊(默認服務器),並且該過程重複自身,因此是重定向循環。

爲了解決這個問題改變你的配置文件,並添加第二個服務器模塊:

server { 
    listen 80; 
    listen [::]:80 ipv6only=on; 

    server_name www.example.com; 

    #rest of your config 
    [...] 

    } 

server { 
    server_name example.com; 
    return 301 $scheme://www.example.com$request_uri; 
    } 

80端口可以被排除在外,因爲它是在默認情況下,如果你想知道。

同樣的原理適用於443的流量(在重定向塊中您必須給出端口和ssl的特定配置)。