nginx - 无法让 nginx 反向代理加载 Web 应用程序的 css/图像

标签 nginx reverse-proxy

我配置了反向代理,当 http 加载时,登录页面的 css/图像不会加载。它试图从本地主机而不是上游服务器加载它们

我尝试了多个 proxy_redirects 和重写(尽管我在这方面还很新),但似乎无法让它工作。

 server {
  listen 80;
  location /test {
    proxy_pass https://10.10.10.10/platform/login;
    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_redirect https://10.10.10.10/ /;
   }




}

日志错误:

[error] 26072#0: *127 open() "/usr/share/nginx/html/platform/images/leaves.png" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET /platform/images/leaves.png HTTP/1.1", host: "localhost", referrer: "http://localhost/test"

当我检查 chrome 上的元素时,它也会为所有 css/图像返回 404。

请帮忙

最佳答案

您网站的 HTML 正在尝试访问不是 /test/** 而是 /platform/images/leaves.png 的网址,这意味着 NGINX 不会尝试使用反向代理。

NGINX 配置的这一部分除了 /test/** 之外根本不被使用,并且 NGINX 正在网络服务器的本地磁盘上搜索不存在的文件.

尝试使用捕获所有范围的配置,而不仅仅是/test。

server {
  listen 80;
  location /platform {
    proxy_pass https://10.10.10.10/platform/;
    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_redirect https://10.10.10.10/ /;
   }
}

您的网站应该可以从 http://{NGINX server}/platform/login 看到 这将更改您的端点的网址,以便您可以为 /test 添加异常(exception),以使 http://{NGINX server}/test 正常工作。

server {
  listen 80;
  location /platform {
    proxy_pass https://10.10.10.10/platform/;
    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_redirect https://10.10.10.10/ /;
   }
  location /test {
    proxy_pass https://10.10.10.10/platform/login;
    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_redirect https://10.10.10.10/ /;
  }
}

您还可以将 HTML 页面和 proxy_pass /test/** 编辑为 https://10.10.10.10/platform/

您的问题在Nginx defaults to /usr/share/nginx/html中有进一步解释。

关于nginx - 无法让 nginx 反向代理加载 Web 应用程序的 css/图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877915/

相关文章:

django - Heroku 是否剥离传入的 X-Forwarded-Proto header ?

http - 为什么 httputil.NewSingleHostReverseProxy 在某些 www 站点上会导致错误?

linux - 当 php fpm 进程崩溃时会发生什么?

nginx - 为什么在 Nginx 中使用 with return 指令时速率限制不起作用?

php - docker 将Apache容器提交到图像带来麻烦

python - 无法在同一服务器上运行 2 个不同的 Flask 应用程序

amazon-web-services - 创建AWS Application Load Balancer规则,该规则可修剪请求的前缀,而无需使用其他反向代理,例如nginx,httpd

grails - Grails使用反向代理重定向

docker - 无法通过dockerized Traefik访问dockerized Neo4j Web界面

asp.net-mvc - 如何使用 Microsoft.AspNetCore.Authentication.Google 强制 HTTPS 回调?