Menu

Why the Django admin says 403 behind a proxy, and the one line that fixes it
📰

Why the Django admin says 403 behind a proxy, and the one line that fixes it

DEV Community: python·John Bannister·6 days ago
#6qvwqNF6
#devtopython#devto#dev#django#environ
Reading 0:00
15s threshold

So you put a Django app behind a reverse proxy that handles HTTPS (nginx, Traefik, Caddy, a PaaS router, a load balancer), open /admin/ , type the right username and password, and get this:

Forbidden (403) CSRF verification failed. Request aborted.

Origin checking failed - https://myapp.example does not match any trusted origins.

Enter fullscreen mode

Exit fullscreen mode

The username and password are fine (checked umpteen times). Django just thinks the request came in over plain HTTP.

Why it happens

The proxy normally ends HTTPS and passes the request on to Django over HTTP, adding a header that says what the original scheme was:

X-Forwarded-Proto: https

Enter fullscreen mode

Exit fullscreen mode

Django will ignore that header unless you tell it to trust it. So request.is_secure() is False , and Django expects the browsers Origin to be http://myapp.example .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.