Nginx反向代理location和proxy_pass配置规则详细总结

2022-12-08 0 1,009

目录一、location配置规则1.匹配模式及顺序举例2.location 是否以“/”结尾二、proxy_pass配置规则补充:Nginx配置proxy_pass转发的/路径问题总结

一、location配置规则

1.匹配模式及顺序举例location = /uri=开头表示精确匹配,只有完全匹配上才能生效location ^~ /uri^~开头对URL路径进行前缀匹配,并且在正则之前location ~ pattern~开头表示区分大小写的正则匹配location /uri不带任何修饰符,也表示前缀匹配,但是在正则匹配之后,如果没有正则命中,命中最长的规则location /通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

2.location 是否以“/”结尾

在 ngnix 中 location 进行的是模糊匹配

没有“/”结尾时,location /abc/def 可以匹配 /abc/defghi 请求,也可以匹配 /abc/def/ghi 等而有“/”结尾时,location /abc/def/ 不能匹配 /abc/defghi 请求,只能匹配 /abc/def/anything 这样的请求

二、proxy_pass配置规则

(1)配置 proxy_pass 时,当在后面的 url 加上了 /,相当于是绝对路径,则 Nginx 不会把 location 中匹配的路径部分加入代理 uri。

(2)如果配置 proxy_pass 时,后面没有 /,Nginx 则会把匹配的路径部分加入代理 uri。

例如:

server { listen 8081; server_name localhost; location / { root html; index index.html index.htm; }#情景1:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾一致) #访问地址:http://localhost:8081/WCP.Service/wcp/modeladapter/download/asc.shtml #最终代理:http://10.194.171.7:13082/modeladapter/download/asc.shtmllocation /WCP.Service/wcp/modeladapter/download/ { proxy_pass http://10.194.171.7:13082/modeladapter/download/; } #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/model/asc.shtmllocation /model/ { proxy_pass http://127.0.0.1:8082/model/; }#情景2:proxy_pass后有/ ,表绝对路径,不把匹配部分加入最终代理路径(location 和proxy_pass结尾不一致) #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtmllocation /model/ { proxy_pass http://127.0.0.1:8082/; }#情景3:proxy_pass后没有 / ,Nginx会把匹配部分带到代理的url #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/model/asc.shtmllocation /model/ { proxy_pass http://127.0.0.1:8082; } #情景4 #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/AAAmodel/asc.shtmllocation /model/ { proxy_pass http://127.0.0.1:8082/AAA; }#情景5 #访问地址:http://localhost:8081/model/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtmllocation /model { proxy_pass http://127.0.0.1:8082/; }#情景6 #访问地址:http://localhost:8081/modelBBB/asc.shtml #最终代理:http://127.0.0.1:8082/asc.shtmllocation /model { proxy_pass http://127.0.0.1:8082/; } location /opus-front-sso { proxy_pass http://10.194.170.94/opus-front-sso; }location /awater { proxy_pass http://10.194.170.94/awater; } }

补充:Nginx配置proxy_pass转发的/路径问题

在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走。

location ^~ /static_js/{proxy_cache js_cache;proxy_set_header Host js.test.com;proxy_pass http://js.test.com/;}

如上面的配置,如果请求的url是http://servername/static_js/test.html

会被代理成http://js.test.com/test.html

而如果这么配置

location ^~ /static_js/{proxy_cache js_cache;proxy_set_header Host js.test.com;proxy_pass http://js.test.com;}

则会被代理到http://js.test.com/static_js/test.htm

总结

到此这篇关于Nginx反向代理location和proxy_pass配置规则的文章就介绍到这了,更多相关Nginx location和proxy_pass配置规则内容请搜索悠久资源以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Nginx服务器 Nginx反向代理location和proxy_pass配置规则详细总结 https://www.u-9.cn/jiaocheng/nginx/3986.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    文章总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

注册会员,众多资源免费下载