Django 'CSRF Verification Failed' in Production: Fix
Login or forms work locally but return 403 'CSRF verification failed. Origin checking failed' on your live Django site? Fix CSRF_TRUSTED_ORIGINS, the proxy header and cookie settings.
"403 Forbidden: CSRF verification failed. Request aborted. Origin checking failed - https://example.com does not match any trusted origins" means the Origin header sent by the browser does not match what Django thinks its own origin is. Behind nginx or a load balancer that terminates HTTPS, the usual fix is three settings: CSRF_TRUSTED_ORIGINS with the scheme, SECURE_PROXY_SSL_HEADER, and a proxy that forwards X-Forwarded-Proto and Host.
This error often appears right after moving from a dev server to production, or after upgrading to Django 4.0 or newer. Django 4.0 added origin verification to the CSRF middleware, so apps that worked for years can start failing after an upgrade. Here is how to find the cause in order.
Step 1: Read the Exact Message
Django tells you which check failed. The text on the 403 page or in the log decides everything:
Origin checking failed - https://example.com does not match any trusted origins.The Origin header was rejected. Go to Step 2.Referer checking failed - no Referer.ordoes not match any trusted originson an HTTPS site. Go to Step 2 and Step 5.CSRF cookie not set.The browser did not send the cookie back. Go to Step 4.CSRF token missing.orincorrect.The form or request did not include the token. Go to Step 6.
Copy the origin from the message exactly. It includes the scheme, the host and the port, and you need all three.
Step 2: Add the Full Origin to CSRF_TRUSTED_ORIGINS
Since Django 4.0, every entry in CSRF_TRUSTED_ORIGINS must include the scheme. A bare domain no longer works.
# settings.py
CSRF_TRUSTED_ORIGINS = [
"https://example.com",
"https://www.example.com",
]
Common mistakes:
- Writing
example.comwithouthttps://. Django 4.0 and newer rejects or ignores it. - Using the wrong name. The setting is
CSRF_TRUSTED_ORIGINS. A Django ticket shows a user adding the origin toCSRF_ALLOWED_ORIGINS, which Django does not read. - Forgetting the
wwwversion, or a staging domain, or a non-default port such ashttps://example.com:8443. - A missing comma between two strings, which Python silently joins into one. A Django forum thread shows this in
ALLOWED_HOSTS, where'3.15.27.43,' 'localhost'became a single bad entry.
For many subdomains you can use a leading wildcard, for example "https://*.example.com".
Step 3: Tell Django the Request Was HTTPS
If nginx, a load balancer or a platform router terminates TLS, Django receives plain HTTP. It then builds its own origin as http://example.com and compares it with the browser's https://example.com. That is the mismatch. Reports on AWS ELB with gunicorn describe exactly this.
Make the proxy send the original scheme and host:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Then tell Django to trust that header:
# settings.py
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
Only turn this on if your proxy always sets or overwrites the header. If Django is reachable directly from the internet, a client could fake it. Keep gunicorn bound to 127.0.0.1 or a private network.
Step 4: Check the Cookie Settings
If the message is CSRF cookie not set, the browser dropped or never stored the cookie. On an HTTPS site, set:
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
Problems to look for:
CSRF_COOKIE_SECURE = Truewhile the site is still served over plain HTTP. The browser will not store a secure cookie.CSRF_COOKIE_DOMAINset to a domain that does not match the one in the address bar.- Cookies blocked in an iframe or a cross-site embed. This needs
SameSite=None; Secureand is a different design problem. - A CDN or cache that serves a cached form page with an old token, or strips
Set-Cookie. Do not cache pages that contain forms with{% csrf_token %}.
Check in browser dev tools, Application, Cookies. You should see a csrftoken cookie for your domain after loading the page.
Step 5: Check Host and Referer Handling
On HTTPS, Django checks the Referer header for secure requests when there is no usable Origin. A missing or stripped Referer causes "Referer checking failed". Look for:
- A privacy setting, an extension or a proxy that removes the Referer. A forum thread about Android browsers shows how one client can fail while others work.
- A proxy that rewrites
Host. If your app depends on the forwarded host, setUSE_X_FORWARDED_HOST = Trueonly when the proxy setsX-Forwarded-Host, and also confirmALLOWED_HOSTSlists your real domain.
Step 6: Fix AJAX, API and Cross-Site Requests
If you post from JavaScript, Django still wants the token:
function getCookie(name) {
const match = document.cookie.match(new RegExp("(^|; )" + name + "=([^;]*)"));
return match ? decodeURIComponent(match[2]) : null;
}
await fetch("/api/orders/", {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": getCookie("csrftoken"),
},
body: JSON.stringify({ item: 1 }),
});
If a separate frontend on another domain calls your Django API with session cookies, add that frontend origin to CSRF_TRUSTED_ORIGINS and configure CORS with credentials. If the endpoint is a webhook from Stripe or another service, do not disable CSRF for the whole site. Use @csrf_exempt on that one view and verify the signature instead. See Stripe Webhook Failures for that pattern.
Step 7: Catch It Before Users Do
CSRF failures are quiet. The user sees a 403 page, assumes the site is broken and leaves. You see nothing unless you look at logs. After any change to HTTPS, domains, a proxy or a Django upgrade, test login and one form post on the live domain. Add an alert on a rise in 403 responses from your login and checkout routes. For the general method, see Find Production Bugs Before Users Report Them.
How Relia Helps
A CSRF 403 on login is a failure users hit and rarely report. Relia watches the APIs and users of a live product, joins related signals such as a deploy and a jump in 403s on one route into one incident explained in plain words, and shows who was affected. After you ship a fix, it checks whether the failures stopped. Relia has a Python package, and you can add uptime checks on key pages such as the login page. There is a Free plan, and a live demo at app.tryrelia.com/demo.
FAQ
What does "Origin checking failed" mean in Django?
The browser sent an Origin header (for example https://example.com) that does not match the origin Django computed for the request or any entry in CSRF_TRUSTED_ORIGINS. Behind a TLS proxy it is usually a scheme mismatch.
Why did CSRF start failing after upgrading to Django 4?
Django 4.0 started verifying the Origin header, and it requires a scheme in CSRF_TRUSTED_ORIGINS. See the Django ticket for a report of this.
Is it safe to disable the CSRF middleware to make it work?
No. It removes protection for every logged-in user. Fix the origin and proxy settings, or use @csrf_exempt on a single webhook view that verifies a signature.
Why does it only fail on mobile or one browser?
Some clients send no Referer or block cookies. Compare the request headers and the error message for the failing client.
Do I need SECURE_PROXY_SSL_HEADER with Docker or a platform router?
Yes, if TLS ends before Django and Django sees HTTP. Make sure the proxy sets X-Forwarded-Proto and that Django is not directly exposed.