From 9d91da6fc681d1bd45d108816f0ed9c1286c5272 Mon Sep 17 00:00:00 2001 From: Keith Smith Date: Sat, 31 Oct 2026 09:19:27 -0600 Subject: [PATCH] Fix /health/ 400ing when hit directly over loopback (as upgrade.sh does) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scripts/upgrade.sh's health check connects straight to gunicorn's bind address, bypassing the reverse proxy entirely, so the request's Host header is "127.0.0.1:" and it has no X-Forwarded-Proto header. Against prod settings that's exactly a DisallowedHost (ALLOWED_HOSTS only has the public domain) — which Django turns into a 400 — and would have then hit a redirect-to-HTTPS loop from SECURE_SSL_REDIRECT right after, since there's no TLS listener on that port to redirect to. Allow the loopback addresses in ALLOWED_HOSTS (harmless: gunicorn's bind address is never reachable from outside this host per docs/install.md) and exempt /health/ from the HTTPS redirect specifically. Co-Authored-By: Claude Sonnet 5 --- config/settings/prod.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config/settings/prod.py b/config/settings/prod.py index 00a1b47..91ea2b5 100644 --- a/config/settings/prod.py +++ b/config/settings/prod.py @@ -3,6 +3,14 @@ from .base import env DEBUG = False +# Health checks (scripts/upgrade.sh, an external uptime monitor, ...) hit +# gunicorn directly over loopback, bypassing the reverse proxy entirely — +# that request's Host header is "127.0.0.1:", not the public domain +# in ALLOWED_HOSTS, so without this Django 400s it. Harmless to always +# allow: gunicorn's bind address is never reachable from outside this host +# in the first place (see docs/install.md's "Reverse proxy" section). +ALLOWED_HOSTS = [*ALLOWED_HOSTS, "localhost", "127.0.0.1", "::1"] # noqa: F405 + # A reverse proxy (nginx, Caddy, Traefik, Nginx Proxy Manager on another # host, ...) is required in front of gunicorn in every deployment — see # docs/install.md — and it's always the one terminating TLS. This tells @@ -10,6 +18,11 @@ DEBUG = False # sets, and the redirect/cookie/HSTS settings below assume it's set. SECURE_SSL_REDIRECT = env.bool("SECURE_SSL_REDIRECT", default=True) SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") +# /health/ is also exempted from the HTTPS redirect above: hit directly +# over loopback (see ALLOWED_HOSTS above) it has no X-Forwarded-Proto +# header to prove it's "secure", and there's no TLS listener on gunicorn's +# own port to redirect to anyway. +SECURE_REDIRECT_EXEMPT = [r"^health/$"] # WhiteNoise serves /static/ directly from gunicorn instead of relying on # the reverse proxy to alias the staticfiles directory on disk — the proxy