nginx反向代理靜態檔案相對傳遞

nginx反向代理靜態檔案相對傳遞

我使用 nginx 進行了簡單的反向代理設定(它在帶有 certbot 的 docker 容器中運行)https://github.com/umputun/nginx-le)作為我的應用程式運行的網路example.com伺服器192.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 '';
    }
}

以及像這樣定義靜態資源的網頁<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 '';
    }
}

相關內容