简体   繁体   English

在Nginx后面通过HTTPS访问Django Admin

[英]Accessing Django Admin over HTTPS behind Nginx

I've got django running in uwsgi behind nginx. 我在dginx后面的uwsgi中运行django。 When I try to access https://site/admin/ I get the expected login screen. 当我尝试访问https://site/admin/我得到了预期的登录屏幕。 Logging in via the form seems to succeed, however, I simply end up back at the login screen. 通过表单登录似乎成功,但是,我只是回到登录屏幕。 Firebug shows a redirect to the plain http://site/admin/ url which is then redirectec by nginx to the https url. Firebug显示重定向到普通的http://site/admin/ url,然后通过nginx将redirectec重定向到https网址。

Help! 救命! I'm confused as to how to force the admin app to use only https urls. 我很困惑如何强制管理应用程序只使用https网址。

Note this seems to be a related, unanswered question: https://example.com/admin redirects to https://admin in Django Nginx and gunicorn 请注意,这似乎是一个相关的,未回答的问题: https://example.com/admin重定向到https://管理员在Django Nginx和gunicorn

Adding the following to nginx.conf fixed the issue for me. 在nginx.conf中添加以下内容为我解决了这个问题。

location / {
    ...
    include                 uwsgi_params;
    uwsgi_param             HTTP_X_FORWARDED_PROTOCOL https;
    uwsgi_param             UWSGI_SCHEME   $scheme;
}

Along with adding the following to settings.py: 除了将以下内容添加到settings.py:

SESSION_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
CSRF_COOKIE_SECURE = True

the following should be all you need to have all traffic to the admin app redirected to https 以下应该是将管理员应用程序的所有流量重定向到https所需的全部内容

location /site/admin/ {
  rewrite ^ https://$host/$request_uri permanent;
}

If that doesn't work, can you post your actual nginx config bits? 如果这不起作用,你可以发布你的实际nginx配置位吗? Can't really suggest more then that without your actual config to look at. 如果没有你的实际配置,那么真的无法提出更多建议。

Update for Django 1.8 settings.py: Django 1.8 settings.py的更新:

SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_REDIRECT_EXEMPT = [r'^(?!admin/).*']

And for your developement rig you may want to overwrite SECURE_SSL_REDIRECT = False in your local settings. 对于您的开发工具,您可能希望在本地设置中覆盖SECURE_SSL_REDIRECT = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM