我正在尝试将所有对我的域的请求重新定向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 的也有相同的结果。