Error message
Django DisallowedHost: Invalid HTTP_HOST header
Django compares the Host header of every request with ALLOWED_HOSTS. When it doesn’t match, Django answers 400 and logs an error, and the Sentry SDK turns that log line into an event. In production, most of these come from bots and probes rather than from your users, but one of them can be a real domain you forgot to allow.
Examples run on 21 November 2026 with Python 3.13.18, Django 6.1.3 and sentry-sdk 2.11.0, 2.40.0 and 2.75.0. Errilo is in private beta.
What the message says
Django raises DisallowedHost, a SuspiciousOperation, when the Host header of a request is refused. The visitor gets a plain Bad Request (400) page. Two messages, observed with Django 6.1.3:
| Case | Message |
|---|---|
The host is well formed but not in ALLOWED_HOSTS | Invalid HTTP_HOST header: 'evil.test'. You may need to add 'evil.test' to ALLOWED_HOSTS. |
| The host is not a valid domain name | Invalid HTTP_HOST header: 'bad host:80'. The domain name provided is not valid according to RFC 1034/1035. |
Where it comes from
Django checks the host in HttpRequest.get_host(). CommonMiddleware, enabled in a standard project, calls it at the start of every request: in our test the stack is django.core.handlers.exception, then django.middleware.common.process_request, then django.http.request.get_host. None of your code has run yet.
The Host header is whatever the client chose to send. A request with an unexpected host typically comes from:
- Scanners and bots that call your server by its IP address or with random names. This is the bulk of it in production, and a normal part of being on the internet.
- Health checks from a load balancer or an orchestrator that call your instance by its internal address, so the header carries an IP address or a service name.
- A proxy that doesn’t forward the original
Host(or forwards another one). - A real domain you forgot in
ALLOWED_HOSTS, after adding a domain or a subdomain. Your users get the 400 page: this is the case you don’t want to miss in the noise.
Minimal reproduction
A project that only allows example.com:
# settings.py (excerpt)
DEBUG = False
ALLOWED_HOSTS = ["example.com"]
MIDDLEWARE = ["django.middleware.common.CommonMiddleware"]Two requests through the Django test client, which sends the Host header you give it:
from django.test import Client
client = Client(raise_request_exception=False)
client.get("/", HTTP_HOST="evil.test").status_code # 400
client.get("/", HTTP_HOST="example.com").status_code # 200The second request is fine. The first is answered with a 400, and Django logs the error with its stack trace to the django.security.DisallowedHost logger.
What the Sentry SDK captures
With nothing but sentry_sdk.init(), every refused request produces one event. It doesn’t come from the Django integration that reports unhandled exceptions: Django catches DisallowedHost itself and builds the 400 response, so nothing crashes. It comes from the logging integration, which the SDK enables by default and which turns records at ERROR level into events. What we observed, identical with 2.11.0, 2.40.0 and 2.75.0:
- level
error, loggerdjango.security.DisallowedHost, exceptionDisallowedHost, mechanismlogging; - no
requestsection: it is empty in our test. The log record’s extra data has the request’s repr andstatus_code: 400, nothing about the host that was refused beyond the message; - a stack trace that contains only Django frames, none of yours.
Django’s own LOGGING setting doesn’t change that: with a NullHandler on this logger and propagate off, the same two events still reached the server. The SDK listens to the logging call, not to the handlers.
Fixes
Allow the hosts that are yours. A leading dot allows a domain and all its subdomains, the port is ignored, and addresses and internal names go in the list as they are. We checked that example.com, www.example.com, a.b.example.com, example.com:8000, 10.0.0.5:8000 and internal-api get a 200 with this list, and evil.test a 400:
# settings.py
ALLOWED_HOSTS = [
".example.com", # example.com and every subdomain
"10.0.0.5", # an address your own probes call, port ignored
"internal-api", # a name used inside your network
]Keep the rest out before Django. Answer requests for unknown hosts at your reverse proxy or load balancer (a default server that refuses them), so scanners never reach the application. We haven’t tested a proxy configuration here.
Filter the noise in the SDK. Django still answers 400 either way; you only decide whether Errilo hears about it. ignore_logger silences one logger: with it, we received no event for the same requests, with all three SDK versions.
# settings.py
import sentry_sdk
from sentry_sdk.integrations.logging import ignore_logger
ignore_logger("django.security.DisallowedHost")
sentry_sdk.init(
dsn="https://<key>@ingest.errilo.com/<project>",
)To keep the logger but drop its events, before_send works too: in our test the events were dropped, and the SDK only sent a client report about them.
def before_send(event, hint):
if event.get("logger") == "django.security.DisallowedHost":
return None
return event
sentry_sdk.init(
dsn="https://<key>@ingest.errilo.com/<project>",
before_send=before_send,
)Filtering also hides the case of a real domain missing from ALLOWED_HOSTS. Keep a check that sees the 400s of your real hostname, such as an uptime check on your public URL.
How Errilo shows it
The issue is titled DisallowedHost: Invalid HTTP_HOST header: …, with no culprit: Errilo takes it from the topmost frame of your code, and there is none. Errilo groups on the exception type and the frames of the stack trace, not on the message: running its fingerprint on our two events, which have different messages and hosts, gave the same fingerprint, so every refused host lands in one issue. Each occurrence keeps its own message, with the host that was refused. Setup for this framework is in the Django guide.
Errilo has no inbound filters on the server: to not receive these events at all, drop them in the SDK as above. If you keep them, one issue with a count shows how much scanning your server gets, and an issue whose messages carry your own domain is the signal that ALLOWED_HOSTS is wrong.