nginx リバース プロキシ 静的ファイル 相対パス

nginx リバース プロキシ 静的ファイル 相対パス

私は、Certbotを使ってDockerコンテナで実行するnginxを使ったシンプルなリバースプロキシをセットアップしました。https://github.com/umputun/nginx-le) をウェブサーバーとしてオンにしexample.com192.168.0.220:80アプリが実行される場所に設定しています。設定は次のとおりです:

server {
    listen   443 ssl;
    server_name example.com;
    charset utf-8;

    ssl_certificate         SSL_CERT;
    ssl_certificate_key     SSL_KEY;
    ssl_trusted_certificate SSL_CHAIN_CERT;

    location /smarthome/ {
        proxy_pass http://192.168.0.220:80/;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Origin '';
    }
}

そして、静的リソースが次のように定義されたWebページ<script src="/js/home.js"></script>

問題は、アクセスしたときにexample.com/smarthome静的リソースが読み込まれないことです。コンソールで:

https://example.com/js/home.js/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome/smarthome net::ERR_TOO_MANY_REDIRECTS

しかし、統計リソースフォームを取得することを期待していましたhttps://example.com/smarthome/js/home.jsが、リダイレクト ループに陥っているようです。

非常に単純なことを見逃しているかもしれませんが、解決策が見つからずイライラしています。ご協力ありがとうございます!

答え1

Nginx の設定に基づいて、ルートの場所を定義する必要があります。そうしないと、ループが常にリダイレクトされます。

設定ファイルは次のように作成できます。

server {
    listen   443 ssl;
    server_name example.com;
    charset utf-8;

    ssl_certificate         SSL_CERT;
    ssl_certificate_key     SSL_KEY;
    ssl_trusted_certificate SSL_CHAIN_CERT;

    location / {
        proxy_pass http://192.168.0.220:80;
    }

    location /smarthome/ {
        proxy_pass http://192.168.0.220:80/;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Origin '';
    }
}

関連情報