NGINX配置之二: nginx location proxy_pass 后面的url 加与不加/的区别.
先给出结果
(1)^~开头是前缀匹配,location后面加 / 也是前缀匹配,只不过匹配范围比不加 / 要小
(2)proxy_pass 端口后面没有 / ,则会将 location 及其后缀的内容完全拼接到 proxy_pass 所配置的地址后
(3)proxy_pass 端口后面有 /,则仅会将 location 的后缀拼接到 proxy_pass 所配置的地址后
这里我们分4种情况讨论
这里我们请求的网站为:192.168.1.123:80/static/a.html

server{ port 80, server name 192.168.1.123 location /static{ proxy_pass 192.168.2.321:81 } location /static{ proxy_pass 192.168.2.321:81/ } location /static/{ proxy_pass 192.168.2.321:81 } location /static/{ proxy_pass 192.168.2.321:81/ }

#192.168.1.123->server name # :80 ---------> port #/statc ------->location #/a.html ------>proxy_pass location /static{ proxy_pass 192.168.2.321:81 }

最后网址经过nginx转向到的网址是 192.168.2.321:81/static/a.html

#192.168.1.123---->server name # :80 ------------> port #/statc ---------->location #/a.html --------->proxy_pass location /static{ proxy_pass 192.168.2.321:81/ }

最后网址经过nginx转向到的网址是 192.168.2.321:81/a.html

#192.168.1.123-->server name # :80 ------------> port #/statc/ ---------->location #a.html --------->proxy_pass location /static/{ proxy_pass 192.168.2.321:81 }

最后网址经过nginx转向到的网址是 192.168.2.321:81/static/a.html

#192.168.1.123-->server name # :80 ------------> port #/statc/ ---------->location(path1) #a.html --------->proxy_pass (path2) location /static/{ proxy_pass 192.168.2.321:81/ }

最后网址经过nginx转向到的网址是 192.168.2.321:81/a.html
总结:
从这四种我们可以的看出,当nginx里面匹配时可以把端口后的参数分为path1+path2(其中我在上方标注的location属于path1,proxy_pass属于path2)
当proxy_pass
端口后面有/时,nginx最后匹配的网址是 proxy_pass的内容加上path2(location的后缀)
端口后面没有/时,nginx最后匹配的网址是 proxy_pass的内容加上path1+path2(location及其后缀的内容)
参考链接:https://blog.csdn.net/s_156/article/details/124059367
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 我与微信审核的“相爱相杀”看个人小程序副业
· DeepSeek “源神”启动!「GitHub 热点速览」
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
2022-01-12 SpringBoot使用令牌桶算法+拦截器+自定义注解+自定义异常实现简单的限流
2017-01-12 Java 集合系列06之 Vector详细介绍(源码解析)和使用示例