django
disallowedhost error
http_host header
allowed_hosts
error troubleshooting

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.

Practice system design

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:

python
1ALLOWED_HOSTS = [
2    "example.com",
3    "www.example.com",
4    "api.example.com",
5]

If you are developing locally, include the local addresses you use:

python
1ALLOWED_HOSTS = [
2    "localhost",
3    "127.0.0.1",
4]

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:

python
1import os
2
3ALLOWED_HOSTS = os.environ.get(
4    "DJANGO_ALLOWED_HOSTS",
5    "localhost,127.0.0.1"
6).split(",")

Then configure production with something like:

bash
export DJANGO_ALLOWED_HOSTS=example.com,www.example.com,api.example.com

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:

python
ALLOWED_HOSTS = ["*"]

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_HOSTS controls which host headers Django accepts'
  • CORS controls which browser origins may call the app
  • 'CSRF_TRUSTED_ORIGINS controls 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:

  1. Copy the exact host value from the error message
  2. Add that hostname if it is legitimate
  3. Check whether a reverse proxy or ingress rewrites the host unexpectedly
  4. Restart or redeploy after changing settings
  5. 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_HOSTS with CORS or CSRF_TRUSTED_ORIGINS.
  • Updating settings.py but not restarting the deployed application.

Summary

  • 'DisallowedHost means Django rejected the request host because it is not in ALLOWED_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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design