Nginxconfiguration不断抛出一个redirect循环,同时试图将wwwredirect到非www和https

我正尝试将所有www请求redirect到我的网域,而不是wwwurl,并且所有内容都是https 。 这是我的nginxconfiguration:

 server { listen 80; server_name example.com www.example.com; location '/.well-known' { default_type "text/plain"; root /home/letsencrypt; allow all; } return 301 https://example.com$request_uri; } server { listen 443 ssl http2; server_name www.example.com; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; location '/.well-known' { default_type "text/plain"; root /home/letsencrypt; allow all; } return 301 https://example.com$request_uri; } server { listen 443 ssl http2; server_name example.com; access_log /var/log/nginx/slmun.access.log; error_log /var/log/nginx/slmun.error.log; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; location '/.well-known' { default_type "text/plain"; root /home/letsencrypt; allow all; } location / { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forward-Proto http; proxy_set_header X-Nginx-Proxy true; proxy_redirect off; } } 

问题是,我在这里得到一个redirect循环。 浏览器说, example.com redirected you too many times. 。 有什么想法发生了什么?

https://slmun.org的curl:

 <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.10.3</center> </body> </html> 

这是访问日志条目:

 162.158.165.25 - - [20/Mar/2017:07:50:53 +0530] "GET / HTTP/1.1" 301 185 "-" "curl/7.47.0" 

奇怪的https也被重新定向。

slmun.org的curl(这是真正的领域):

 <html> <head><title>301 Moved Permanently</title></head> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>nginx/1.10.3</center> </body> </html> 

这是访问日志条目:

 162.158.165.25 - - [20/Mar/2017:07:51:49 +0530] "GET / HTTP/1.1" 301 185 "-" "curl/7.47.0" 

www的也有相同的结果。

你没有提到你在你的问题中使用CloudFlare。 我相信问题是与CloudFlare,无论是caching的结果或PageRule。 清除CloudFlarecaching,并在执行此诊断时禁用任何页面规则。

下一个选项是您的代理应用程序发送redirect。 检查Nginx的访问日志和代理应用程序以确认。

如果你没有这个好运气,你可以发布你的CloudFlareconfiguration和你的Nginxconfiguration文件截图,以及代理configuration的日志。

以下是使用Live HTTP Headers插件的Firefox的请求/响应标头。 我可以确认一个循环redirect。

 https://slmun.org/ GET / HTTP/1.1 Host: slmun.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br DNT: 1 Connection: keep-alive Upgrade-Insecure-Requests: 1 

响应

 HTTP/2.0 301 Moved Permanently Date: Mon, 20 Mar 2017 02:56:33 GMT Content-Type: text/html Set-Cookie: __cfduid=d8f2af34526d1d1beae191bf7de70440a1489978593; expires=Tue, 20-Mar-18 02:56:33 GMT; path=/; domain=.slmun.org; HttpOnly Location: https://slmun.org/ Server: cloudflare-nginx cf-ray: 342566200f8418f0-AKL X-Firefox-Spdy: h2