nginx 重写 url 而不会导致重定向和代理到后端应用程序
nginx rewrite url without causing redirect and proxy to backend app
我正在尝试在 nginx 中实现以下目标:
在 http://<nginx-host>:81/admin/metrics
上点击 nginx 并让 nginx 将其代理到 http://127.0.0.1:8001
,/admin/metrics
将 重写为 /metrics
。实际代理的管理应用程序将收到 http://127.0.0.1:8001/metrics
.
因此,http://<nginx-host>:81/admin/metrics
=> http://127.0.0.1:8001/metrics
,并且 没有 导致 redirect
。
这是我目前拥有的:
#admin app
upstream admin_backend {
server 127.0.0.1:8001;
keepalive 16;
}
#host /admin paths on another port
server {
listen 81;
#pass everything to backend admin app that matches /admin/* with /admin/ removed
location ~ /admin/ {
limit_except GET HEAD POST PUT { }
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_pass http://admin_backend;
rewrite ^/admin/(.*)$ / last;
}
}
这是行不通的。 nginx 没有将请求代理回 admin
应用程序并不断返回 404
.
我在这里错过了什么?
我在 nginx users forum 上交叉发布了这个问题,并从 nanaya
那里得到了解决方案(谢谢!)。
基本上,我需要做的就是在 rewrite
指令中将 last
替换为 break
:
所以,
rewrite ^/admin/(.*)$ / last;
成为
rewrite ^/admin/(.*)$ / break;
重定向api和文件请求到后端
location ~ /api/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server:8009;
rewrite ^/api/(.*)$ / break;
}
我正在尝试在 nginx 中实现以下目标:
在 http://<nginx-host>:81/admin/metrics
上点击 nginx 并让 nginx 将其代理到 http://127.0.0.1:8001
,/admin/metrics
将 重写为 /metrics
。实际代理的管理应用程序将收到 http://127.0.0.1:8001/metrics
.
因此,http://<nginx-host>:81/admin/metrics
=> http://127.0.0.1:8001/metrics
,并且 没有 导致 redirect
。
这是我目前拥有的:
#admin app
upstream admin_backend {
server 127.0.0.1:8001;
keepalive 16;
}
#host /admin paths on another port
server {
listen 81;
#pass everything to backend admin app that matches /admin/* with /admin/ removed
location ~ /admin/ {
limit_except GET HEAD POST PUT { }
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_pass http://admin_backend;
rewrite ^/admin/(.*)$ / last;
}
}
这是行不通的。 nginx 没有将请求代理回 admin
应用程序并不断返回 404
.
我在这里错过了什么?
我在 nginx users forum 上交叉发布了这个问题,并从 nanaya
那里得到了解决方案(谢谢!)。
基本上,我需要做的就是在 rewrite
指令中将 last
替换为 break
:
所以,
rewrite ^/admin/(.*)$ / last;
成为
rewrite ^/admin/(.*)$ / break;
重定向api和文件请求到后端
location ~ /api/ {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server:8009;
rewrite ^/api/(.*)$ / break;
}