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

查看:1619
本文介绍了静态文件上的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服务器上应相同.在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/;      
}


P.S.


P.S.

我通过代理探索了许多使用nginx config的操作. 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来检测upsteam

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天全站免登陆