我使用 Nginx 作为网络服务器,使用 gunicorn django 服务器的反向代理。
我尝试使用此处的 SSLRedirect 片段:
http://djangosnippets.org/snippets/85/
因为此代码段在我的设置中总是会从 is_secure()
返回 false,从而导致重定向循环,所以我不得不进行一些更改。
SSL 有效,但当我访问 http://domain.net/main
时,它不会重定向到 https://domain.net/main
。它不应该那样做吗?
下面概述了我所做的修改:
if 'HTTP_X_FORWARDED_PROTOCOL' in request.META:
return True
在我的 nginx conf 中(我只需要 SSL,不需要 http):
server {
listen 8888;
server_name domain.net;
ssl on;
ssl_certificate /path/to/domain.pem;
ssl_certificate_key /path/to/domain.key;
# serve directly - analogous for static/staticfiles
location /media/ {
root /path/to/root;
}
location /static/ {
root /path/to/root;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://127.0.0.1:8881/;
# note this line
proxy_set_header X-Forwarded-Protocol https;
}
}
最佳答案
只需完全使用 nginx 即可。根本不需要涉及 Django:
server {
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
server {
listen 443;
# The rest of your original server config here
}
关于python - Django SSL Redirect 片段修改,未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265824/