Configure SSL for Django
Django does not handle certificates. TLS is done by the web server or load balancer in front of it, such as Nginx, Apache or a cloud load balancer. What Django does control is how the application behaves once HTTPS is in place: redirects, cookies, HSTS and how it detects a secure request. Getting these wrong causes redirect loops, broken CSRF checks and cookies that travel over plain HTTP.
Django SSL, step 1: TLS in front of the app
Run Django under Gunicorn or uWSGI and put Nginx in front with a certificate. A free certificate works well; see getting one with Certbot. The proxy must pass the original scheme to Django:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}Django SSL, step 2: trust the forwarded header
In settings.py, add the setting below. Only use it if your proxy always sets or strips this header; otherwise a client could send a fake one and make an HTTP request look secure.
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
USE_X_FORWARDED_HOST = False
CSRF_TRUSTED_ORIGINS = ["https://www.example.com"]Since Django 4.0, CSRF_TRUSTED_ORIGINS entries must include the scheme. A missing or schemeless entry is a frequent cause of "CSRF verification failed" errors after moving to HTTPS, especially on admin logins.
Step 3: Redirect and secure cookies
| Setting | Recommended | Effect |
|---|---|---|
| SECURE_SSL_REDIRECT | True | Redirects HTTP requests to HTTPS |
| SESSION_COOKIE_SECURE | True | Session cookie only over HTTPS |
| CSRF_COOKIE_SECURE | True | CSRF cookie only over HTTPS |
| SECURE_HSTS_SECONDS | Start small | Adds Strict-Transport-Security |
| SECURE_CONTENT_TYPE_NOSNIFF | True (default) | Blocks MIME sniffing |
Set SECURE_SSL_REDIRECT = True only after SECURE_PROXY_SSL_HEADER works. If the redirect is on and Django cannot see that the request was already HTTPS, every request redirects forever. If your load balancer does its own redirect, you can leave the Django one off and exempt health check paths using SECURE_REDIRECT_EXEMPT.
Step 4: Roll out HSTS carefully
HSTS tells browsers to refuse HTTP for your domain. Begin with a short value, for example SECURE_HSTS_SECONDS = 300, confirm nothing breaks, then raise it. Add SECURE_HSTS_INCLUDE_SUBDOMAINS only when every subdomain serves valid HTTPS, and SECURE_HSTS_PRELOAD only if you intend to submit the domain to the preload list, which is difficult to reverse. Read how to enable HSTS first.
Step 5: Run the deployment check
Django has a built-in audit of these settings:
python manage.py check --deployIt prints warnings such as a missing HSTS value, insecure cookies or DEBUG left on. Treat it as a checklist, not a guarantee. It cannot see your proxy or certificate.
Local HTTPS for development
If you need HTTPS locally, the django-extensions package offers runserver_plus --cert-file cert.pem, and Django's own runserver has no TLS option. Keep local test certificates out of your repository, and read how to protect SSL private keys for real ones.
Troubleshooting
- Redirect loop: the proxy header is missing or not trusted in
SECURE_PROXY_SSL_HEADER. - CSRF failures: check
CSRF_TRUSTED_ORIGINSincludes the https:// origin. - Mixed content warnings: templates or static settings still build http:// URLs; check
STATIC_URLand hard-coded links. - Wrong host in emails: verify the Sites framework domain and
Hostheader pass-through.
To test from outside, run curl -I http://example.com and expect a 301 to HTTPS, then use the free SSL chain checker to confirm the chain is complete. Automate renewals using Certbot renewal hooks so the proxy reloads when certificates change.
Behind a CDN or load balancer
If a CDN sits in front, there may be two TLS hops: visitor to CDN and CDN to your origin. Use a mode that validates the origin certificate rather than one that accepts anything. The origin should present a valid certificate for the hostname the CDN requests, whether that is a public one or a CDN-issued origin certificate. Django then only needs the forwarded-proto header the CDN or load balancer sets.
Also keep ALLOWED_HOSTS tight. It is not a TLS setting, but a wildcard there together with USE_X_FORWARDED_HOST lets a client choose the host Django believes it is serving.
Related reading: SSL for Python Flask, free SSL on nginx and enabling HSTS.
Frequently Asked Questions
Does Django need a certificate of its own?
No. TLS is handled by Nginx, Apache or a load balancer in front of Django. Django only needs to know that the request arrived over HTTPS.
Why do I get a redirect loop after enabling HTTPS?
Django sees plain HTTP from the proxy and redirects to HTTPS again. Set SECURE_PROXY_SSL_HEADER so it trusts the forwarded protocol header.
How do I stop cookies travelling over HTTP?
Set SESSION_COOKIE_SECURE and CSRF_COOKIE_SECURE to True, then redirect HTTP at the proxy so the cookie is never sent in clear text.
✅ Post-Installation Verification & SEO Checklist
After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:
- Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
- Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
- Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
- Confirm both root and
wwwversions resolve cleanly to one canonical HTTPS URL. - Verify your HTTPS property and
sitemap.xmlinside Google Search Console and Bing Webmaster Tools.