Accessing Django Admin over HTTPS behind Nginx

admin, django, https, nginx, redirect

Solution

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

Problem

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

Original source