Django / Gunicorn と Nginx リバース プロキシ - ベース パスへのリダイレクトが間違っている

Django / Gunicorn と Nginx リバース プロキシ - ベース パスへのリダイレクトが間違っている

私のドメインのサブパス (example.com/myapp) の下で、Gunicorn/Uvicorn を使用して Django アプリを正常に実行しています。

Nginx vhost は次のようになります。

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
}

Gunicorn の起動コマンドを使用した systemd サービスは次のとおりです。

[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

続きを読むNginxのサブフォルダ内のDjangoについて、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')

アプリとそのサブパスに次のようにアクセスできます。https://example.com/myapp/エンドポイントDjango管理ページhttps://example.com/myapp/admin(以下のように書き換えられましたhttps://example.com/myapp/admin/login/?next=/admin/) 例えば。

しかし、Django管理ページの検証ボタンをクリックすると、誤ってベースパスにリダイレクトされます(https://example.com/admin/login/?next=/admin/また、すべての静的ファイルに対して404が返されますが、それらはベースパスの下でも提供されます。https://example.com/static/xxxサブパスの代わりにhttps://example.com/myapp/static/xxxただし、vhost では正しく設定されていると思っていました。

私はSOで同様の問題を見てきました(これは非常によく似たもの)、しかしそれは役に立ちませんでした。

何か案が?

関連情報