use*_*752 6 proxy reverse-proxy nginx
我正在寻找将一个URL /路径反向代理到端点上代表不同服务器托管其自己的Web应用程序的不同端口的方法。
我可以使用proxy_pass,但是静态文件失败,因为资源是相对于其实例的。
我有例如-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;
}
Run Code Online (Sandbox Code Playgroud)
如上所述,反向代理的工作原理非常好,除了与app2相关联的静态文件外。App1静态文件可以正常工作,但App2静态文件会生成404。这很有意义,因为App1资源文件位于/assets/app1.css此文件中,因为我有/适当的位置重定向,可以解析回App1但App2资源文件,这完全是/assets/app2.css404中的结果不同。
因此,有没有办法将App2静态请求从重写/assets/app2.css为它们各自的代理位置?就像是:
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;
}
Run Code Online (Sandbox Code Playgroud)
当一个文件/assets/app1.css由从规则(应用1)负载location /app1/然后加载如/assets/app1.css通过从规则location /。App2 具有相同的行为,但您location /配置的是 App1,而不是 App2。
您的配置必须是:
location /app1/ {
proxy_pass: http://192.168.1.1:8080/app1/;
}
location /app2/ {
proxy_pass: http://192.168.1.1:8081/app1/;
}
Run Code Online (Sandbox Code Playgroud)
必需:别名 app1 在代理服务器和上游服务器上应该是相同的。在 upsteam 服务器上,它可能是原始 webroot 应用程序的符号链接。
或者您可以使用不同的子域或端口....
server_name app1.localhost;
location / {
proxy_pass: http://192.168.1.1:8081/;
}
Run Code Online (Sandbox Code Playgroud)
聚苯乙烯
我通过代理探索了许多使用 nginx 配置的操作。Nginx 不能通过一个规则正常工作:
location /app1/ {
proxy_pass: http://192.168.1.1:8080/;
}
Run Code Online (Sandbox Code Playgroud)
例如: css 和 js 文件将被加载 - proxy_server/css ...
-proxy_server/js ...
从请求中proxy_server/app1/index.html,您将获得 404。
您可以将location /css/规则添加到配置中。但是你的 app2 也可以使用这个位置。并且您无法检测到上游服务器要由他代理。您可以使用 reffer 检测上游
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/;
}
}
Run Code Online (Sandbox Code Playgroud)
但 POST 数据将在重定向后被销毁。
如果只需要按位置调整代理服务器的配置就好了。但这是一个梦想。
| 归档时间: |
|
| 查看次数: |
8329 次 |
| 最近记录: |