Django DisallowedHost and CSRF errors behind a proxy
THE SHORT ANSWER
DisallowedHost means the request's Host header isn't in ALLOWED_HOSTS, so Django answers 400; add your production domain to the list. The CSRF 403 "Origin checking failed - https://... does not match any trusted origins" usually means Django thinks the request came over plain HTTP: the proxy ended HTTPS and forwarded it as HTTP, so Django expects an http:// origin and the browser sent https://. Set SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") when your proxy sets that header. Since Django 4.0, entries in CSRF_TRUSTED_ORIGINS must include the scheme, such as https://example.com.
What do the two errors look like?
The first one shows up as a plain Bad Request (400) page on every URL, with this in the logs:
Invalid HTTP_HOST header: 'myapp.ethiodeploy.app'. You may need to add 'myapp.ethiodeploy.app' to ALLOWED_HOSTS.
The second only appears on forms and other POST requests, the admin login being the usual first victim. The browser shows Forbidden (403), “CSRF verification failed. Request aborted.”, and the log names the reason:
Forbidden (Origin checking failed - https://myapp.ethiodeploy.app does not match any trusted origins.): /admin/login/
Both pass on localhost because runserver sees the request directly over HTTP, and with DEBUG = True and an empty ALLOWED_HOSTS, Django accepts .localhost, 127.0.0.1 and [::1]. In production you have a real domain, DEBUG = False, and a proxy between the browser and Django.
How do I fix DisallowedHost?
ALLOWED_HOSTS lists the host names your site answers to. Django compares each request's Host header against it, ignoring the port, and raises DisallowedHost when nothing matches. Django's docs on DEBUG: with it off and ALLOWED_HOSTS unset, “all requests” are returned as “Bad Request (400)”. Read the list from the environment so each deploy can have its own:
# settings.py
import os
ALLOWED_HOSTS = [h for h in os.environ.get("ALLOWED_HOSTS", "").split(",") if h]ALLOWED_HOSTS=myapp.ethiodeploy.app,example.com,www.example.com
- Use host names only: no
https://, no path, no port. The error message quotes the exact value Django saw, so copy it from there. - A leading dot matches the domain and every subdomain:
.example.comcoversexample.comandwww.example.com. "*"matches anything, and the docs say you are then responsible for validating theHostheader yourself. Don't use it to make the error go away.- Keep the platform address in the list after adding a custom domain if your host's health checks use it.
Why does CSRF fail only behind a proxy?
Since Django 4.0, the CSRF middleware checks the browser's Origin header on POST requests. In Django's source, it builds the origin it expects from two parts: https if request.is_secure() is true, otherwise http, plus the host from request.get_host(). If that string equals the Origin header, the request passes. If not, Django falls back to CSRF_TRUSTED_ORIGINS, and when nothing matches you get “Origin checking failed”.
Now add a proxy. Most platforms end HTTPS at a load balancer and forward the request to your app over plain HTTP. Django sees an HTTP connection, so is_secure() is false and the expected origin becomes http://myapp.ethiodeploy.app. The browser, which really is on HTTPS, sends Origin: https://myapp.ethiodeploy.app. One letter apart, and the request is rejected. That is why the error message shows an https:// origin that looks exactly like your site.
So the fix is usually not CSRF_TRUSTED_ORIGINS at all. It is telling Django the request was secure.
How do I set SECURE_PROXY_SSL_HEADER?
Proxies that end HTTPS normally pass the original scheme in an X-Forwarded-Proto header. Tell Django to read it:
# settings.py
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = TrueThe header name is written the way it appears in request.META: upper case, dashes turned into underscores, with HTTP_ in front. When the value is https, or a comma-separated list whose leftmost entry is https, is_secure() returns true, the expected origin becomes https://... and same-site form posts pass.
Django's docs warn that this setting can compromise your site if the proxy doesn't control the header. Set it only when all of these are true: your app is behind a proxy, the proxy strips any X-Forwarded-Proto a client sends, and the proxy sets the header itself. If a client could reach Django directly with a forged header, it could claim to be on HTTPS.
Fixing is_secure() also fixes other things that read it: request.build_absolute_uri() starts generating https:// links, and SECURE_SSL_REDIRECT = True stops redirecting in a loop, since without the header Django thinks every request is HTTP.
When do I need CSRF_TRUSTED_ORIGINS?
When a POST legitimately comes from an origin other than the one being served: a frontend on app.example.com posting to api.example.com, or a proxy setup where Django can't see the real host or scheme. Django 4.0 changed the format. The release notes: “Values in the CSRF_TRUSTED_ORIGINS setting must include the scheme (e.g. 'http://' or 'https://') instead of only the hostname.”
# Django 4.0 and later
CSRF_TRUSTED_ORIGINS = [
"https://example.com",
"https://*.example.com", # every subdomain
]An old entry without a scheme fails a system check, so manage.py commands such as migrate and runserver refuse to run:
?: (4_0.E001) As of Django 4.0, the values in the CSRF_TRUSTED_ORIGINS setting must start with a scheme (usually http:// or https://) but found example.com. See the release notes for details.
The old .example.com form for subdomains became https://*.example.com. A common way to keep both settings in step is to derive the origins from ALLOWED_HOSTS:
CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS if not h.startswith(".")]Adding your own domain here also makes the 403 go away when SECURE_PROXY_SSL_HEADER is missing, because the https:// origin now matches the trusted list. That hides the real problem: is_secure() is still false, so absolute URLs still say http://. Fix the proxy header first.
Do I need USE_X_FORWARDED_HOST?
Only if your proxy replaces the Host header. Some setups forward every request with the upstream's internal name, such as Host: 127.0.0.1:8000 or a container name, and put the original in X-Forwarded-Host. Then DisallowedHost names an internal address, and the CSRF check compares the browser's origin with that internal host.
You can fix it in either place. In the proxy, pass the original host through (in Nginx, proxy_set_header Host $host;). Or in Django:
USE_X_FORWARDED_HOST = True
Django then prefers X-Forwarded-Host over Host, and ALLOWED_HOSTS is checked against it. The docs say to enable it only “if a proxy which sets this header is in use”. USE_X_FORWARDED_PORT does the same for the port; USE_X_FORWARDED_HOST takes priority when both are on. If the proxy already keeps the original Host, leave both off.
The 403 names a different reason
The text in parentheses tells you which check failed. The other common ones:
CSRF cookie not set.The browser didn't send thecsrftokencookie, for example because the form page came from a different domain than the one it posts to.CSRF token missing.The form has no{% csrf_token %}, or a JavaScript call doesn't send theX-CSRFTokenheader.Referer checking failed - no Referer.On a secure request without anOriginheader, Django requires aReferer. AReferrer-Policy: no-referrerheader on your pages can strip it.Referer checking failed - https://... does not match any trusted origins.The same mismatch as the Origin case, on a request that sent only aReferer.
What does EthioDeploy need?
HTTPS ends at EthioDeploy's proxy, which forwards the request to your container over HTTP with an X-Forwarded-Proto header and keeps the original Host header. So a Django app here needs two settings and no more:
ALLOWED_HOSTSwith your<project-slug>.ethiodeploy.appaddress and any custom domain, set as an environment variable.SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https").
USE_X_FORWARDED_HOST isn't needed, since the host arrives unchanged. Add CSRF_TRUSTED_ORIGINS only for cross-origin posts.
The deploy health check is sent with Host: <project-slug>.ethiodeploy.app. If that address is missing from ALLOWED_HOSTS, Django answers the check with a 400 and the deploy fails, even when your custom domain is listed. The full settings file is in the Django deploy guide; static files are covered in Django static files 404 in production.
Questions
The request's Host header isn't in ALLOWED_HOSTS, so Django raised DisallowedHost and answered 400. Add the host name the message quotes, without scheme or port, to ALLOWED_HOSTS.
Behind a proxy that ends HTTPS, Django sees plain HTTP and expects an http:// origin while the browser sends https://. Set SECURE_PROXY_SSL_HEADER to trust the proxy's X-Forwarded-Proto header.
Yes, since Django 4.0. Each entry must start with a scheme, such as https://example.com, and a subdomain wildcard is written https://*.example.com. Entries without a scheme fail system check 4_0.E001.
Only if you validate the Host header yourself, which Django's docs say becomes your responsibility. Otherwise list the real domains.
Only when your proxy replaces the Host header and sends the original in X-Forwarded-Host. If the proxy keeps the original Host, leave it off.
Sources
Checked on November 15, 2026:
Push a repo. Get a URL.
Start freeJoin the EthioDeploy Telegram
Release notes as they ship, plus deploy help from the team and other developers.