I have a Django app successfully running with Gunicorn/Uvicorn, under a subpath of my domain (example.com/myapp).
The Nginx vhost looks like this:
server {
server_name example.com;
location /myapp/ {
proxy_pass http://myapp_gunicorn/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /myapp/static/ {
alias /home/www/myapp/static/;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
upstream myapp_gunicorn {
server 127.0.0.1:8002;
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name example.com;
listen 80;
return 404; # managed by Certbot
}
Here is the systemd service with the start command of Gunicorn:
[Unit]
Description=myapp gunicorn daemon
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/www/myapp
ExecStart=/home/venvs/myapp/bin/gunicorn myapp.asgi:application -b 0.0.0.0:8002 -w 8 -k uvicorn.workers.UvicornWorker --log-file /home/www/myapp.log
[Install]
WantedBy=multi-user.target
Reading on SO about Django in subfolder with Nginx, I added the following settings to my Django settings.py:
FORCE_SCRIPT_NAME = '/myapp'
USE_X_FORWARDED_HOST = True
# To make Django trusts the https from the Nginx proxy
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
I can successfully access my app and its subpaths like https://example.com/myapp/endpoint and the Django admin page https://example.com/myapp/admin (successfully rewritten as https://example.com/myapp/admin/login/?next=/admin/) for example.
However, when clicking on the validation button Django admin page, I get wrongly redirected to the base path (https://example.com/admin/login/?next=/admin/).
Also, I get a 404 for all the static files, they are also served under the base path https://example.com/static/xxx instead of the subpath https://example.com/myapp/static/xxx, although I thought it was correctly setup in the vhost.
I have been looking at similar issues on SO (including this very similar one), but it didn't help me.
Any idea?