nginx反向代理静态文件相对传递

nginx反向代理静态文件相对传递

我使用 nginx 设置了简单的反向代理(它在带有 certbot 的 docker 容器中运行https://github.com/umputun/nginx-le) 作为 Web 服务器,我的应用程序在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 '';
    }
}

并且静态资源的网页定义如下<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 '';
    }
}

相关内容