Gau*_*nia 9 django ssl https nginx
我正在尝试使用SSL在本地测试我的Django应用程序.我有@login_required装饰者的视图.所以当我点击时/locker,我会被重定向到/locker/login?next=/locker.这适用于http.
但是,每当我使用https时,重定向会以某种方式丢弃安全连接,所以我会得到类似的东西 https://cumulus.dev/locker -> http://cumulus.dev/locker/login?next=/locker
如果我直接https://cumulus.dev/locker/login?next=locker通过安全连接打开该页面.但是一旦我输入用户名和密码,我就会回去http://cumulus.dev/locker.
我正在使用Nginx处理SSL,然后与之交谈runserver.我的nginx配置是
upstream app_server_djangoapp {
server localhost:8000 fail_timeout=0;
}
server {
listen 80;
server_name cumulus.dev;
access_log /var/log/nginx/cumulus-dev-access.log;
error_log /var/log/nginx/cumulus-dev-error.log info;
keepalive_timeout 5;
# path for static files
root /home/gaurav/www/Cumulus/cumulus_lightbox/static;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
}
server {
listen 443;
server_name cumulus.dev;
ssl on;
ssl_certificate /etc/ssl/cacert-cumulus.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
access_log /var/log/nginx/cumulus-dev-access.log;
error_log /var/log/nginx/cumulus-dev-error.log info;
keepalive_timeout 5;
# path for static files
root /home/gaurav/www/Cumulus/cumulus_lightbox/static;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server_djangoapp;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Django 仅在代理后面运行在纯 HTTP 上,因此它将始终使用它来构造绝对 URL(例如重定向),除非您配置它如何查看代理请求最初是通过 HTTPS 发出的。
从 Django 1.4 开始,您可以使用该SECURE_PROXY_SSL_HEADER设置来执行此操作。当 Django 看到配置的 header 时,它会将请求视为 HTTPS 而不是 HTTP:request.is_secure()将返回 true,https://将生成 URL,等等。
但是,请注意文档中的安全警告:您必须确保代理替换或删除所有传入客户端请求(HTTP 和 HTTPS)中的可信标头。上面的 nginx 配置并没有做到这一点X-Forwarded-Ssl,使其具有欺骗性。
对此的传统解决方案是在每个代理配置中根据需要X-Forwarded-Protocol设置为http或。https然后,您可以使用以下命令配置 Django 来查找它:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1578 次 |
| 最近记录: |