Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Django Admin over HTTPS behind Nginx

I've got django running in uwsgi behind nginx. When I try to access https://site/admin/ I get the expected login screen. 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.

Help! I'm confused as to how to force the admin app to use only https urls.

Note this seems to be a related, unanswered question: https://example.com/admin redirects to https://admin in Django Nginx and gunicorn

like image 791
justinzane Avatar asked Aug 13 '12 23:08

justinzane


2 Answers

Adding the following to nginx.conf fixed the issue for me.

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

Along with adding the following to settings.py:

SESSION_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
CSRF_COOKIE_SECURE = True
like image 188
justinzane Avatar answered Nov 06 '22 19:11

justinzane


the following should be all you need to have all traffic to the admin app redirected to https

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

If that doesn't work, can you post your actual nginx config bits? Can't really suggest more then that without your actual config to look at.

like image 35
cobaco Avatar answered Nov 06 '22 18:11

cobaco