静态文件上的 Nginx 反向代理 404 [英] Nginx reverse proxy 404 on static files

查看:74
本文介绍了静态文件上的 Nginx 反向代理 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将一个 url/路径反向代理到端点上的不同端口,这些端口代表托管自己的 Web 应用程序的不同服务器.

I am looking to reverse proxy one url/path to different ports on an end point that represent different servers hosting their own web apps.

我有 proxy_pass 工作,但静态文件失败,因为资源是相对于它们的实例.

I have the proxy_pass working, but the static files fail because the resources are relative their instance.

例如我有 - server_name = myproxy.com:

I have for example - server_name = myproxy.com:

location /app1/{
    proxy_pass: http://192.168.1.1:8080/;
    proxy_set_header Host 192.168.1.1;
}
location /app2/{
    proxy_pass: http://192.168.1.1:8081/;
    proxy_set_header Host 192.168.1.2;
}
location /{
    proxy_pass: http://192.168.1.1:8080/;
    proxy_set_header Host 192.168.1.1;
}

如上所述,反向代理工作得很好,除了与 app2 关联的静态文件.App1 静态文件工作正常,但 App2 静态文件导致 404.这是有道理的,因为 App1 资源文件位于 /assets/app1.css 之所以有效,是因为我对位置 / 到位,解析回 App1 但 App2 资源文件,它们完全不同 /assets/app2.css 导致 404.

The reverse-proxy works great as mentioned, except for the static files associated with the app2. App1 static files work fine, but App2 static files result in a 404. Which make sense, because App1 resource files are located at /assets/app1.css this works because I have a redirect for location / in place that resolves back to App1 but App2 resource files, which are totally different /assets/app2.css result in 404.

那么,有没有办法将来自 /assets/app2.css 的 App2 静态请求重写到它们各自的代理位置?类似:

So, is there a way to rewrite App2 static requests from /assets/app2.css to their respective proxy location? something like:

location /app1/{
    proxy_pass: http://192.168.1.1:8080/;
    proxy_set_header Host 192.168.1.1;
}
location /app2/{
    proxy_pass: http://192.168.1.1:8081/;
    proxy_set_header Host 192.168.1.2;

    *rewrite app2 static urls frome /assets/* to /app2/assets/*
}
location /{
    proxy_pass: http://192.168.1.1:8080/;
    proxy_set_header Host 192.168.1.1;
}

推荐答案

当文件 /assets/app1.css(App1) 按规则从 location/app1/ 然后通过 location/ 的规则加载为 /assets/app1.css.App2 具有相同的行为,但您的 location/ 是为 App1 配置的,而不是为 App2 配置的.

When a file /assets/app1.css(App1) load by rules from location /app1/ then load as /assets/app1.css by rules from location /. App2 has same behavior, but you location / was configurate for App1, not for App2.

您的配置必须是:

location /app1/ {
    proxy_pass: http://192.168.1.1:8080/app1/;        
}

location /app2/ {
    proxy_pass: http://192.168.1.1:8081/app1/;      
}

必需:别名 app1 在代理服务器和上游服务器上应相同.在 upsteam 服务器上,它可能是原始 webroot 应用程序的符号链接.

Required: The alias app1 should be is same on proxy and upsteam servers. On upsteam server it may been symlink to original webroot application.

或者您可以使用不同的子域或端口....

Or you can use different subdomain or ports....

server_name app1.localhost;
location / {
    proxy_pass: http://192.168.1.1:8081/;      
}

<小时>

附言

我通过代理探索了许多使用 nginx 配置的操作.Nginx 不能通过一个规则正常工作:

I explored many manipulations with nginx config by proxy. Nginx doesn't work correctly with one rule:

location /app1/ {
    proxy_pass: http://192.168.1.1:8080/;        
}

例如:css 和 js 文件将被加载- proxy_server/css ...- proxy_server/js ...

For example: css and js files will be load - proxy_server/css ... - proxy_server/js ...

来自请求 proxy_server/app1/index.html,你会得到 404.

from request proxy_server/app1/index.html and you will get 404.

您可以将 location/css/ 规则添加到配置中.但是你的 app2 也可以使用这个位置.并且您无法检测到上游服务器要由他代理.您可以使用 reffer 检测上游

You can add location /css/ rule to config. But your app2 could use this location too. And you can't detect upstream server to proxy by him. You can use reffer to detect upsteam

 server {
    listen          80;

    if ($uri ~ app1/) {
      break;
    }

    if ($http_referer ~ app1/ ) {
      rewrite (.*) /app1/$1 redirect;
    }

    location /app1/ {
        proxy_pass http://192.168.1.1:8080/;
    }
}

但是重定向后POST数据会被销毁.

but POST data will be destroy after redirect.

如果只需要按位置调整配置到代理服务器就好了.但这是一个梦想.

It would be great if it was necessary to adjust the configuration only to the proxy server by location. But it's a dream.

这篇关于静态文件上的 Nginx 反向代理 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆