How to fix Django error DisallowedHost at / Invalid HTTP_HOST header ... You may need to add ... to ALLOWED_HOSTS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
DisallowedHost means Django received a request whose Host header does not match anything in ALLOWED_HOSTS. This is a security check, not a random deployment annoyance, so the fix is to allow the correct hostnames explicitly rather than disabling the protection.
Why Django Rejects the Request
Django validates the incoming Host header to protect against host header attacks. If the request says it is for api.example.com, but your settings only allow example.com, Django raises DisallowedHost before normal view logic runs.
That means this is fundamentally a configuration issue. The application code can be fine and still fail immediately because the request host is not trusted.
Typical causes include:
- the production domain is missing from
ALLOWED_HOSTS - a load balancer forwards a host you did not expect
- you are testing with an IP address or alternate port
- local development is hitting the app through a hostname not listed
Fix ALLOWED_HOSTS Correctly
The direct fix is to add the hostnames your app should actually serve:
If you are developing locally, include the local addresses you use:
If you access the development server by another hostname, add that too. Django checks the host value the browser actually sends, not what you meant.
Environment Variables Make This Easier
Hard-coding hosts is fine for small projects, but production deployments often vary by environment. A common pattern is to load the list from an environment variable:
Then configure production with something like:
This keeps development, staging, and production settings separate without editing source code for every deployment.
Do Not Use ["*"] as a Lazy Fix
You can make the error disappear with:
but that should not be your default production answer. It disables the host restriction entirely and defeats the point of the check.
For temporary debugging inside a private environment, you may see people do it briefly, but the real fix is to list the actual hosts your app serves.
Reverse Proxies and Load Balancers Matter
A common production surprise is that the request does not arrive with the public hostname you expected. If Nginx, Apache, a cloud load balancer, or an ingress controller forwards requests with a different host, Django validates that forwarded host.
So when debugging, inspect the real request path:
- What hostname is the browser using
- What hostname does the proxy forward
- What hostname does Django see
If you are behind a proxy and the host value is wrong, the problem may be proxy configuration rather than Django settings.
ALLOWED_HOSTS Is Separate from CORS and CSRF
This is another easy confusion.
- '
ALLOWED_HOSTScontrols which host headers Django accepts' - CORS controls which browser origins may call the app
- '
CSRF_TRUSTED_ORIGINScontrols trusted origins for CSRF protection'
You may need to configure more than one of these, but fixing CORS or CSRF will not solve DisallowedHost if the host header itself is not allowed.
A Practical Debugging Checklist
When you hit this error in a deployed app:
- Copy the exact host value from the error message
- Add that hostname if it is legitimate
- Check whether a reverse proxy or ingress rewrites the host unexpectedly
- Restart or redeploy after changing settings
- Verify the environment variable is really loaded by the running process
That last step matters more than people think. Many "I already added it" situations are just stale deployments or the wrong settings module being used.
Common Pitfalls
- Adding the wrong hostname because you guessed instead of using the exact value from the error.
- Using
ALLOWED_HOSTS = ["*"]in production as a permanent fix. - Forgetting that load balancers, proxies, and ingress rules can change what Django sees.
- Mixing up
ALLOWED_HOSTSwith CORS orCSRF_TRUSTED_ORIGINS. - Updating
settings.pybut not restarting the deployed application.
Summary
- '
DisallowedHostmeans Django rejected the request host because it is not inALLOWED_HOSTS.' - The correct fix is to add the real trusted hostnames your app serves.
- Environment variables are a clean way to manage different host lists across environments.
- Reverse proxies and ingress layers can affect which host Django receives.
- Do not use a wildcard in production unless you fully understand the security tradeoff.
Related reading
- How to fix error where a KerasTensor is passed to a TF API?
- How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session
- How to fix WordPress HTTPS issues when behind an Amazon Load Balancer?
- How to force https on elastic beanstalk?
- How to fix error Cannot register 2 metrics with the same name /tensorflow/api/keras/optimizers
- How to fix initial_lr not specified when resuming optimizer error for scheduler?
- How to fix Docker, Got permission denied issue
- How to fix Docker Permission denied
System design interview problems on this topic
- Design a Load Balancer: solution and walkthrough
- Design a Domain Name System: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.