I am running a Django app behind Nginx in a Docker environment. My problem is sort of the same from this thread: Django returning "CSRF verification failed. Request aborted. " behind Nginx proxy locally.
Upon learning that I have to add CSRF_TRUSTED_ORIGINS
in settings, since I am using Django version 4.X, I added my localhost
CSRF_TRUSTED_ORIGINS = [
'http://localhost',
'http://localhost:3000',
'https://example.com',
]
I can login to my admin when the container is deployed locally. However, when I deployed it to production, I still get the error:
CSRF verification failed. Request aborted.
I deployed it in a Google compute engine. The app does not have a domain name yet. Thus, I visit the app using its external address of the machine: http://XX.XX.XX.XX/admin
. Should I add this address to my CSRF_TRUSTED_ORIGINS
? My understanding is that this is the same as adding the localhost http://localhost
since I am visiting the host machine's address.
CSRF_TRUSTED_ORIGINS = [
'http://localhost',
'http://localhost:3000',
'https://example.com',
'http://XX.XX.XX.XX'
]
What if I deployed it to another machine with a different address? Should I again add it? Is there any other way to allow the CSRF without specifically adding the address since it would be tedious if I would be changing/migrating in host machines?
This is my nginx config, if it is of any help.
upstream api {
server container_name:8000;
}
server {
listen 80;
location / {
proxy_pass http://api;
}
location /static/ {
alias /static/;
}
}