THS-Location斜杠区别详解

1 、location和proxy_pass都带/,则真实地址不带location匹配目录

location /api/ {

proxy_pass http://127.0.0.1:8080/;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/upload

2 、location不带/,proxy_pass带/,则真实地址会带/

location /api {

proxy_pass http://127.0.0.1:8080/;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080//upload

3 、location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/

location /api/ {

proxy_pass http://127.0.0.1:8080;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/api/upload

4 、location和proxy_pass都不带/,则真实地址会带location匹配目录/api/

location /api {

proxy_pass http://127.0.0.1:8080;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/api/upload

5 、同1,但proxy_pass带地址

location /api/ {

proxy_pass http://127.0.0.1:8080/server/;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/server/upload

6 、同2,但proxy_pass带地址,则真实地址会多个/

location /api {

proxy_pass http://127.0.0.1:8080/server/;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/server//upload

7 、同3,但 proxy_pass带地址,则真实地址会直接连起来

location /api/ {

proxy_pass http://127.0.0.1:8080/server;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/serverupload

8 、同4,但proxy_pass带地址,则真实地址匹配地址会替换location匹配目录

location /api {

proxy_pass http://127.0.0.1:8080/server;

}

访问地址:www.test.com/api/upload-->http://127.0.0.1:8080/server/upload

总结:

1、proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录部分去除location匹配目录。

2、proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录部分。

3、有时更改location和proxy_pass后浏览器查看不生效,有可能是启用缓存的原因,此时需要禁用缓存:add_header Cache-Control no-cache;

相关推荐
K·Herbert4 小时前
最新CentOS 7 yum源失效的解决方案(2025年6月)
linux·运维·centos
别骂我h4 小时前
部署KVM虚拟化平台
linux·运维·服务器
showmethetime5 小时前
优化nginx参数(基本通用参数)
运维·nginx
老六ip加速器5 小时前
获取ip地址安全吗?如何获取静态ip地址隔离ip
运维·网络·智能路由器
净心净意13 小时前
浅谈DaemonSet
运维·jenkins
Apex Predator20 小时前
jenkins流水线打包vue无权限
运维·jenkins
哲讯智能科技1 天前
苏州SAP代理商:哲讯科技助力企业数字化转型
大数据·运维·人工智能
十五年专注C++开发1 天前
Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
linux·运维·c++·qt·cmake·跨平台编译
qq_628515761 天前
Centos与RockLinux设置静态ip
linux·运维·centos
程序员老乔1 天前
【Dify系列】【一】【安装与部署】【ubuntu22.04安装docker部署dify1.4.2】
运维·docker·容器