nginx 反向代理URI重写

nginx 反向代理URI重写

如果proxy_pass的URL定向里包括URI,那么请求中匹配到location中URI的部分会被proxy_pass后面URL中的URI替换,eg:

location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}
请求http://example.com/name/test.html 会被代理到http://127.0.0.1/remote/test.html

如果proxy_pass的URL定向里不包括URI,那么请求中的URI会保持原样传送给后端server,eg:

location /name/ {
    proxy_pass http://127.0.0.1;
}

请求http://example/name/test.html 会被代理到http://127.0.0.1/name/test.html

一些情况下,不能确定替换的URI
location里是正则表达式,这种情况下,proxy_pass里最好不要有URI
在proxy_pass前面用了rewrite,如下,这种情况下,proxy_pass是无效的,eg:

location /name/ {
    rewrite    /name/([^/]+) /users?name=$1 break;
    proxy_pass http://127.0.0.1;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
Kuinlan
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
CEO @ 上海秋与网络科技有限公司
文章
1
粉丝
1
喜欢
0
收藏
0
排名:3342
访问:74
私信
所有博文
社区赞助商