Nginx:是啥导致了这个 301 重定向?
Posted
技术标签:
【中文标题】Nginx:是啥导致了这个 301 重定向?【英文标题】:Nginx: What is causing this 301 redirect?Nginx:是什么导致了这个 301 重定向? 【发布时间】:2019-07-26 23:26:03 【问题描述】:我仍然不知道为什么我的网页会显示
myapp.com redirected you too many times.
nginx 仅用作我的 django 频道应用程序的代理,该应用程序与 daphne 一起运行。
Nginx 正在运行,没有错误。
myapp systemd[1]: Starting A high performance web server and a reverse proxy server...
达芙妮正在 127.0.0.1:8001 上运行
curl -I http://myapp.com/
curl -I https://myapp.com/
返回
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 05 Mar 2019 11:53:39 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://myapp.com/
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 05 Mar 2019 11:54:43 GMT
Content-Type: text/html
Content-Length: 194
Location: https://myapp.com/
Connection: keep-alive
有谁知道是什么导致了这个 301 错误?
Nginx 配置文件
server
listen 80;
servername myapp.com www.myapp.com;
servertokens off;
return 301 https://$servername$requesturi;
server
listen 443 ssl; # managed by Certbot
server_name myapp.com www.myapp.com;
ssl_certificate /etc/letsencrypt/live/myapp.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myapp.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
root /home/me/myapp/src/myapp;
location = /favicon.ico access_log off; log_not_found off;
location /static/
root /home/me/myapp/src/myapp;
location /media/
root /home/me/myapp/src/myapp;
location /
try_files $uri/ @python_django;
location @python_django
proxy_pass http://127.0.0.1:8001;
proxy_pass_request_headers on;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
【问题讨论】:
【参考方案1】:我仍然不确定如何完全解决这个问题,但是当我拿出来时
return 301 https://$server_name$request_uri;
然后运行curl -I -L http://www.myapp.com/
我明白了
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Mar 2019 13:33:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 31 Jan 2017 15:01:11 GMT
Connection: keep-alive
ETag: "5890a6b7-264"
Accept-Ranges: bytes
但我无法访问该页面,因为显然 LetsEncrypt 正在将所有 http
重定向到 https
,所以我被引回到 301 错误页面!
【讨论】:
以上是关于Nginx:是啥导致了这个 301 重定向?的主要内容,如果未能解决你的问题,请参考以下文章