我使用Nginx作为Web服务器,并为gunicorn-django服务器提供反向代理。
我尝试使用这里的SSLRecrect片段:
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
}