CSRF verification fails only after putting the app behind a reverse proxy — origin does not match, or the cookie is not set
The application works perfectly when I run it directly and reach it on a local port. Once it is behind a reverse proxy with TLS and a real hostname, any form submission fails CSRF verification.
The error varies. Sometimes it complains that the origin does not match any trusted origins. Sometimes it says the CSRF cookie is not set. The admin login is the easiest place to reproduce it.
I have added the hostname to the allowed hosts setting, which fixed a different error earlier but not this one.
What is the framework actually checking, and what is the complete set of settings I need for this deployment shape?
@proxy_headers_paul · 6h ago
Point four is the one people miss, and it is on the proxy rather than the application, so nobody looks there.
Several proxies default to sending the backend's address as the Host header rather than the one the browser used. The application then sees an internal name, fails the allowed-hosts check or builds the wrong origin, and no amount of adding your public hostname to the application's settings helps because your public hostname never arrives.
It is one line of proxy configuration in every proxy, but it is a different line in each of them. Check yours specifically rather than trusting that it does the obvious thing.
Same family of problem: proxies that strip or rewrite the forwarded protocol header, and chains of two proxies where only the outer one sets it correctly.
Reply
Report