CloudFlare allows you to enable certain page rules , one of which is forced SSL (through which it makes redirects ). This is a great thing to use in addition to django-sslify or django-secure
In addition to setting up SSL forwarding, you also need to tell Django to make secure requests. Luckily, Django provides a decent guide for this, but there are a few things that it doesn't mention, but I had to do with Nginx.
In Django settings you need to tell Django how to define a secure request
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
In your nginx configuration, you need to configure the X-Forwarded-Protocol header (and the X-Forwarded-For / X-Scheme headers are also useful).
proxy_set_header X-Scheme $scheme; proxy_set_header X-Forwarded-Protocol $scheme; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
You also need to proxy the Host header, so Django can read the correct host and port, which is used, among other things, to create absolute URLs and CSRFs.
proxy_set_header Host $http_host;
Note that instead of $host or $host:$server_port I used the $http_host variable. This ensures that Django will still respect CSRF requests on non-standard ports, while maintaining the correct absolute URLs.
Like most things related to nginx and gunicorn, YMMV, and it becomes easier after you do this several times.
source share