nginx - https上的301永久重定向但http工作正常
问题描述
当我跑
curl -I http://myapp.com/
curl -I https://myapp.com/
http
返回
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Mar 2019 17:46:29 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
而https
回报
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Tue, 05 Mar 2019 17:50:29 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://www.myapp.com/
curl: (47) Maximum (50) redirects followed
我显然无法访问该域,我收到错误ERR_TOO_MANY_REDIRECTS
,因为 Let's Encrypt 设置为将任何 http 请求重定向到 https。
我看到两个选项,停用https
并通过访问该站点http
或找出301 Permanent Redirect
在https
.
我最初通过取出线路摆脱301 Permanent Redirect
了http
return 301 https://$server_name$request_uri;
我的 nginx 配置文件
server {
listen 80;
servername myapp.com www.myapp.com;
servertokens off;
}
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;
}
}
我急需帮助,拜托!!!
解决方案
您可以遵循这种替代方法。我发现处理重定向更简单一些。
server {
listen 80;
#listen [::]:80 ipv6only=on;
server_name your.server.com;
access_log /etc/nginx/access.log;
root /var/www/html/someroot;
location / {
#autoindex on;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri =404;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
#proxy_set_header X-NginX-Proxy true;
#proxy_pass http://127.0.0.1:8080/;
#proxy_redirect off;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
#proxy_redirect off;
#proxy_set_header X-Forwarded-Proto $scheme;
#proxy_cache one;
#proxy_cache_key sfs$request_uri$scheme;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/some/fullchain.pem;
# managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/some/privkey.pem;
# managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
注意最后一个 if 子句。那是所有重定向发生的地方。基本上,您只使用一个服务器块并监听来自端口 80 和 443 的所有流量。当有人点击端口 80 时,请求被重定向到 443。对我来说效果很好。让我知道它是否也适合你。
推荐阅读
- android - Firebase Firestore:没有互联网连接时是否有回调?
- c# - 如何获取 Binding 生成的元素的宽度?
- android - 在定义 Android Manifest 中的兼容性时,哪种方式是正确的?
- c# - 我的自动测试失败,因为它无法专注于弹出窗口
- javascript - 如何在 Node.js 服务器和 React 客户端应用程序之间共享 config.file?
- ios - SwiftUI:如何在按钮单击 + API 调用后显示下一个视图
- python - Pandas 聚合中的自定义函数
- linux - 通过电子邮件发送时,输出文本中未显示颜色
- javascript - Webpack - 在已安装包中使用的别名文件夹
- celery - Celery Task.AsyncResult 采用 task_id 但记录为“获取此类任务的 AsyncResult 实例” - 为什么?