我正在尝试将所有对我的域的请求重定向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.
。知道发生了什么吗?
<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 也有同样的结果。
答案1
您在问题中没有提到您正在使用 CloudFlare。我认为问题出在 CloudFlare 上,可能是缓存结果,也可能是 PageRule。在进行此诊断时,请清除 CloudFlare 缓存并禁用所有 PageRule。
下一个选项是您的代理应用程序发送重定向。检查 Nginx 和代理应用程序的访问日志以确认。
如果您对此不满意,请发布您的 CloudFlare 配置和 Nginx 配置文件的屏幕截图以及代理配置的日志。
这是使用 Live HTTP Headers 插件的 Firefox 的请求/响应标头。我可以确认存在循环重定向。
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