Nginx

Nginx 配置在嘗試將 www 重定向到非 www 和 https 時不斷拋出重定向循環

  • March 20, 2017

我正在嘗試將所有對我的域的請求重新定向www到非 www url,並將所有請求同時重定向到https。這是我的 nginx 配置:

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;
 }
}

問題是,我在這裡得到了一個重定向循環。瀏覽器說example.com redirected you too many times.。知道發生了什麼嗎?

https://slmun.org的捲曲:

<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(這是真正的域):

<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,無論是記憶體結果還是 PageRule。在進行此診斷時,清除您的 CloudFlare 記憶體並禁用任何頁面規則。

下一個選項是您的代理應用程序發送重定向。查看 Nginx 和代理應用的訪問日誌確認。

如果您對此不滿意,請發布您的 CloudFlare 配置和 Nginx 配置文件的螢幕截圖,以及代理配置的日誌。

這是來自 Firefox 的帶有 Live HTTP Headers 外掛的請求/響應標頭。我可以確認一個循環重定向。

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

引用自:https://serverfault.com/questions/839309