DisallowedHost: Invalid HTTP_HOST header — Django's 400 Bad Request
Django received a request for a host name that is not in ALLOWED_HOSTS, so it answered 400 Bad Request. Add the domain the site is served on to ALLOWED_HOSTS, without https:// and without a port, or read the list from an environment variable, and redeploy.
By Livops team, the people who build and run LivopsUpdated
What the log shows, while the visitor sees only Bad Request (400):
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'shop.example.com'. You may need to add 'shop.example.com' to ALLOWED_HOSTS.
This page explains the check behind the error, lists the causes from the most common, shows how to test the host check before deploying, and says what Livops sets up for Django.
What does the error mean?
ALLOWED_HOSTS is the list of host names a Django site may serve. It exists to prevent HTTP Host header attacks, and its default is an empty list[1]. When the Host header of a request matches nothing in the list, request.get_host() raises DisallowedHost[2], and Django answers 400.
Locally you rarely see it: with DEBUG = True and an empty list, Django accepts .localhost, 127.0.0.1 and [::1][1]. In production DEBUG is off, and Django does not work at all without a suitable ALLOWED_HOSTS[3].
Causes, most common first
1. The production domain is not in the list
The list is empty or holds only localhost. Add the domain, or better, read the list from the environment so the same code works everywhere:
import os
ALLOWED_HOSTS = [h.strip() for h in os.environ.get("ALLOWED_HOSTS", "").split(",") if h.strip()]
ALLOWED_HOSTS=shop.example.com,www.shop.example.com
2. The entry has a scheme or a port
Entries are matched against the Host header exactly, case-insensitively and without the port[1]. https://shop.example.com and shop.example.com:8000 never match. Write the bare host name.
3. A new domain or subdomain was added
You connected www., a second domain, or a preview address, and only the first one is listed. A value beginning with a dot matches the domain and all its subdomains: .example.com covers example.com and www.example.com[1].
4. The variable is read as one string
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS") gives Django a string, not a list, and no host matches it. Split it on commas, as above.
5. A proxy changes the Host header
Behind a proxy that sends its own internal name as Host, every request is refused. Make the proxy pass the original Host. USE_X_FORWARDED_HOST makes Django prefer X-Forwarded-Host, and should be enabled only if a proxy that sets that header is in use[1].
Avoid ALLOWED_HOSTS = ["*"] as the fix: Django matches anything then, and validating the host becomes your responsibility[1][3].
How to confirm the fix locally
Run with production settings, then send the host the server will receive:
DEBUG=False ALLOWED_HOSTS=shop.example.com python manage.py runserver 127.0.0.1:8000
curl -i -H "Host: shop.example.com" http://127.0.0.1:8000/ # expect 200 or a redirect
curl -i -H "Host: other.example" http://127.0.0.1:8000/ # expect 400
This assumes your settings read DEBUG and ALLOWED_HOSTS from the environment. python manage.py check --deploy automates some of the checklist’s other checks[3].
On Livops
- The variable is set for you. For a Python web application, Livops sets
ALLOWED_HOSTSto the project’s addresses — its own address and the domains you connected — comma-separated, unless you set the variable yourself. Asettings.pythat reads it, as in cause 1, works without changes. - The home page is checked after the deploy. Once the new version is live, Livops opens its home page. If Django refuses the host, the deploy log gets a warning and a Tip: line that names the refused host, the subdomain wildcard you can add instead, and the line that reads the list from
ALLOWED_HOSTS. The deploy is not rolled back for this; the warning is there so you see it before your visitors do. - Migrations run before every start for a project laid out the way
startprojectlays it out, andcollectstaticruns during the build whenSTATIC_ROOTis set. - The assistant explains. It reads the same log and says what to change in
settings.py. Changing your code is yours to do.
The same case in the docs: Django refuses the address, and what Livops does for Django: Frameworks.
Questions
Why does the site work locally and fail in production?
With DEBUG on and ALLOWED_HOSTS empty, Django accepts localhost, 127.0.0.1 and [::1]. In production DEBUG is off, and Django does not work at all without a suitable ALLOWED_HOSTS.
Can I just use ALLOWED_HOSTS = ['*']?
Django allows it, but then validating the Host header is your job, and the setting exists to protect against Host header attacks. List your domains instead, or use a leading dot for all subdomains of one domain.
Should the entry include https:// or the port?
No. Entries are host names only. They are matched against the Host header case-insensitively and without the port, so https://example.com or example.com:8000 never match.
I fixed ALLOWED_HOSTS and now forms fail with a CSRF error. Why?
That is the next check. For POST requests from another origin, or behind some proxies, Django also needs the site's origin, with its scheme, in CSRF_TRUSTED_ORIGINS, for example https://example.com.
Does Livops set ALLOWED_HOSTS?
Yes. For a Python web app it sets the ALLOWED_HOSTS variable to the project's addresses, comma-separated, unless you set it yourself. Your settings.py has to read it, because Django does not read that variable on its own.
Sources
- Django docs — Settings, ALLOWED_HOSTS (docs.djangoproject.com). Checked .
- Django docs — Request and response objects, get_host() (docs.djangoproject.com). Checked .
- Django docs — Deployment checklist (docs.djangoproject.com). Checked .
Put your project online today
Connect GitHub or drop a folder. No card is asked for.
Deploy your Django appRelated
- Gunicorn "Worker failed to boot": what it means and how to fix itGunicorn's worker could not load your application, so the master process shut down. The cause is the Python traceback printed just above the message: most often a missing package or variable, or a wrong module:variable path in the gunicorn command. Fix what the traceback names and the workers boot.
- ModuleNotFoundError: No module named — after a deployPython searched sys.path and could not find the module you import. After a deploy that almost always means the package is not in requirements.txt, is listed under a different name than the one you import, or the start command runs a Python that the packages were not installed into.
- App not listening on $PORT: why the health check failed after deployThe platform started your app, tried the port it gave it, and nothing answered in time. Almost always the app listens on its own fixed port instead of the PORT variable, listens on localhost (127.0.0.1) instead of 0.0.0.0, or crashed or was still starting when the check gave up. Listen on 0.0.0.0 and the port in PORT, and it passes.