Django "Invalid HTTP_HOST header" Behind a Tunnel, Then a CSRF 403
Published on November 16, 2026   •   8 min read

Django "Invalid HTTP_HOST header" Behind a Tunnel, Then a CSRF 403

Jarek CeborskiJarek Ceborski

Django raises DisallowedHost with "Invalid HTTP_HOST header" when the request's Host header is not in ALLOWED_HOSTS. Behind a tunnel the Host is the tunnel's public name, so the fix is to add that domain to ALLOWED_HOSTS.

The first form you submit after that fails as well, with a 403 that reads "CSRF verification failed" and "Origin checking failed". The tunnel ends HTTPS before your app, so the request reaches Django as plain HTTP, and Django expects an http:// origin while the browser sends https://. List the origin in CSRF_TRUSTED_ORIGINS, or let Django read the tunnel's X-Forwarded-Proto header with SECURE_PROXY_SSL_HEADER. In development the second is the better fix, unless you rewrite the Host header.

Both errors were reproduced on 16 November 2026 on a fresh Django 6.1.3 project (startproject, migrate and one superuser) running the dev server on port 8000. Requests went through a LocalCan Public URL (also called a tunnel) from the LocalCan CLI 1.4.0, sent by curl and by headless Chromium 148, and every fix was tested on the admin login form. The commands and numbers come from my own runs.

SetupLogin pageAdmin login POSTScheme and host Django sees
startproject defaults400 on /, DisallowedHostnot triednot tried
ALLOWED_HOSTS200403, Origin checking failednot tried
ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS200302http, public host
ALLOWED_HOSTS and SECURE_PROXY_SSL_HEADER200302https, public host
Host rewrite ({{target_host}}), defaults200403, Origin checking failedhttp, localhost
Host rewrite ({{target_host}}), SECURE_PROXY_SSL_HEADER200403, Origin checking failedhttps, localhost
Host rewrite ({{target_host}}), CSRF_TRUSTED_ORIGINS200302not tried

DisallowedHost: Invalid HTTP_HOST header

With the ALLOWED_HOSTS = [] that startproject writes, the first request through the Public URL got a 400 and Django's yellow debug page, titled "DisallowedHost at /", with this message:

Text
Invalid HTTP_HOST header: 'brave-wolf.localcan.dev'. You may need to add 'brave-wolf.localcan.dev' to ALLOWED_HOSTS.

The dev server logs the same message after a traceback that ends in get_host:

Text
django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: 'brave-wolf.localcan.dev'. You may need to add 'brave-wolf.localcan.dev' to ALLOWED_HOSTS.
[16/Nov/2026 10:02:03] "GET / HTTP/1.1" 400 69619

While DEBUG = True and the list is empty, Django accepts only .localhost, 127.0.0.1 and [::1] (ALLOWED_HOSTS in Django's settings reference↗). A tunnel forwards the hostname the browser used, so Django sees brave-wolf.localcan.dev and refuses it. The tunnel is not needed to reproduce it. The same Host header sent straight to the dev server gets the same 400:

Bash
curl -s -o /dev/null -w "%{http_code}\n" -H "Host: brave-wolf.localcan.dev" http://localhost:8000/
Text
400

Fix: add the tunnel's domain to ALLOWED_HOSTS

A value that starts with a dot matches the domain and all its subdomains, so one entry covers every *.localcan.dev Public URL you create. Add your own domain too if you use a custom one. Keep localhost and 127.0.0.1 in the list, because the DEBUG fallback above stops applying once the list has entries.

mysite/settings.py
Python mysite/settings.py
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '.localcan.dev', '.trylocalcan.com', '.local']

The .trylocalcan.com entry covers Free plan Public URLs, and .local covers LocalCan's local domains. A '*' entry would match any host, and Django's ALLOWED_HOSTS reference↗ then leaves validating the Host header to you. The same fix for Vite, Next.js, Angular, Nuxt, Astro and Rails is on the Invalid Host Header error page.

CSRF verification failed: Origin checking failed

With the hosts fixed, the pages loaded, but submitting the admin login returned a 403 page that said "CSRF verification failed. Request aborted." and gave the reason, which the dev server logged as well:

Text
Forbidden (Origin checking failed - https://brave-wolf.localcan.dev does not match any trusted origins.): /admin/login/
[16/Nov/2026 10:02:31] "POST /admin/login/?next=/admin/ HTTP/1.1" 403 2617

Headless Chromium got the 403 too. The login on http://localhost:8000 got a 302 to /admin/, because there the browser's origin is http://localhost:8000, the one Django expects. Every form POST goes through this check, so the admin login is only the first place you meet it.

Why the Origin check fails behind a tunnel

Since Django 4.0 the CSRF middleware compares the browser's Origin header with the origin it expects (Django 4.0 release notes↗). It builds that origin from the request: https if request.is_secure() is true and http otherwise, then the host (_origin_verified in csrf.py↗).

The tunnel ends HTTPS at its edge, and LocalCan on your machine passes the request to localhost:8000 as plain HTTP. LocalCan's traffic inspector shows the failing request as Django received it:

Bash
localcan traffic get 599bcdf8 --format http --redact
Text
# forwarded to http://localhost:8000/admin/login/?next=/admin/ · original client URL: https://brave-wolf.localcan.dev/admin/login/?next=/admin/
POST /admin/login/?next=/admin/ HTTP/1.1
Host: brave-wolf.localcan.dev
origin: https://brave-wolf.localcan.dev
x-forwarded-host: brave-wolf.localcan.dev
x-forwarded-port: 443
x-forwarded-proto: https
[accept, content-*, cookie, lc-ipcountry, referer, user-agent, x-forwarded-for and x-real-ip trimmed, form body and response cut]

Django ignores x-forwarded-proto unless a setting tells it otherwise, so is_secure() returned false, the expected origin was http://brave-wolf.localcan.dev, and the browser's https://brave-wolf.localcan.dev did not match. Django's docs describe the same trap for any proxy in front of the app, which "may be “swallowing” whether the original request uses HTTPS or not" (SECURE_PROXY_SSL_HEADER↗).

Fix A: list the origin in CSRF_TRUSTED_ORIGINS

mysite/settings.py
Python mysite/settings.py
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '.localcan.dev', '.trylocalcan.com', '.local']
CSRF_TRUSTED_ORIGINS = ['https://*.localcan.dev', 'https://*.trylocalcan.com']

Entries need the scheme, and the subdomain wildcard is https://*.example.com, not the leading dot that ALLOWED_HOSTS uses. Django 4.0 changed that format, and a system check flags values in the old one (CSRF_TRUSTED_ORIGINS changes↗). With this line, the curl login got a 302 to https://brave-wolf.localcan.dev/admin/ and Chromium reached "Site administration".

The CSRF_TRUSTED_ORIGINS line above leaves out LocalCan's .local domains, which are served over HTTPS as well. Their origin carries the port the browser shows. On the Mac used here another app holds port 443, so LocalCan's HTTPS proxy is moved to port 5443 with proxy_https.port, and the login over https://keen-seal.local:5443 still got the 403 with 'https://*.local' added. With 'https://*.local:5443' it got the 302. With the proxy on the default port 443 the browser leaves the port out of the origin, so the entry should be 'https://*.local', a case not tried here.

Django still believes the requests arrive over HTTP. A two-line view that prints request.scheme and request.build_absolute_uri() shows it:

mysite/urls.py
Python mysite/urls.py
from django.contrib import admin
from django.http import HttpResponse
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('scheme/', lambda r: HttpResponse(f'{r.scheme} {r.build_absolute_uri()}\n')),
]
Text
http http://brave-wolf.localcan.dev/scheme/

Any absolute URL Django builds from such a request starts with http://.

Fix B: trust X-Forwarded-Proto with SECURE_PROXY_SSL_HEADER

LocalCan sends X-Forwarded-Proto: https with every request through a Public URL (headers LocalCan adds). Tell Django to read it, and is_secure() returns true:

mysite/settings.py
Python mysite/settings.py
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '.localcan.dev', '.trylocalcan.com', '.local']
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

With this line in place of CSRF_TRUSTED_ORIGINS, the login got its 302 from curl and from Chromium, and the view printed:

Text
https https://brave-wolf.localcan.dev/scheme/

Requests to http://localhost:8000 kept working as plain HTTP, with a 302 on the login. The .local domain needed no extra line: the local proxy sets X-Forwarded-Proto to the scheme of each request (headers LocalCan adds), and the login over https://keen-seal.local:5443 got its 302 too.

Django's docs warn that this setting can compromise your site's security. The proxy in front of Django has to strip X-Forwarded-Proto from all incoming requests "even when it contains a comma-separated list of protocols", and set it only on requests that came in over HTTPS (SECURE_PROXY_SSL_HEADER↗).

A request sent through the Public URL with X-Forwarded-Proto: http reached Django as https, so LocalCan replaced the client's value. A request sent straight to localhost:8000 can set any value. With -H "X-Forwarded-Proto: https", curl got https https://localhost:8000/scheme/ back.

The dev server listens on 127.0.0.1 by default ("Starting WSGI development server at http://127.0.0.1:8000/↗"), so only your own machine can send that last request. Keep the line in your development settings. A production server sits behind a different proxy and needs its own check against the same conditions.

With the original host forwarded, Fix B is the better one, since Django's own URLs become https:// too. With the Host header rewritten, only Fix A works.

If you rewrite the Host header instead

The Invalid Host Header error page offers a second route for DisallowedHost: LocalCan sends localhost as the Host header, so ALLOWED_HOSTS never sees the public name.

Bash
localcan http 8000 -H "Host: {{target_host}}"

On startproject defaults this got past DisallowedHost with no settings change, and the login page loaded. The same page warns that the rewrite "does nothing for checks on the browser's Origin header", and Django's CSRF check is one of them. The login POST failed with "Origin checking failed - https://tender-dog.localcan.dev↗ does not match any trusted origins." Django now builds its URLs on localhost:

Text
http http://localhost/scheme/

Fix A is the one to pair with the rewrite, because CSRF_TRUSTED_ORIGINS matches the browser's origin whatever Host Django sees. With it, the login got its 302 and Chromium reached "Site administration". Fix B does not help here. With SECURE_PROXY_SSL_HEADER the expected origin became https://localhost, which still differs from what the browser sends, and the POST got the same 403.

Check that it's fixed

Submit a form through the Public URL again. The dev server log shows the POST with a 302 or 200 where the Forbidden (Origin checking failed …) line used to be:

Text
[16/Nov/2026 10:03:50] "POST /admin/login/ HTTP/1.1" 302 0

If it still fails, list the exchanges with localcan traffic ls --host <your Public URL> and open the POST with localcan traffic get <id> --format http. The origin line is what the browser sent. Django compares it with the scheme and the Host line, and it takes the scheme from x-forwarded-proto only with Fix B.

More posts