nginx反向代理中神奇的斜线
nginx反向代理中神奇的斜线
在进行nginx反向代理配置的时候,location和proxy_pass中的斜线会造成各种困扰,有时候多一个或少一个斜线,就会造成完全不同的结果,所以特地将location和proxy_pass后有无斜线的情况进行了排列组合,进行了一次完整的测试,找出原理,以提高姿势水平~
〇. 环境信息
两台nginx服务器
nginx A: 192.168.1.48
nginx B: 192.168.1.56
一. 测试方法
- 在nginx A中配置不同的规则,然后请求nginx A:http://192.168.1.48/foo/api
- 观察nginx B收到的请求,具体操作是查看日志中的$request字段
二. 测试过程及结果
案例1
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx B收到的请求:/api
案例2
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx B收到的请求://api
案例3
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx B收到的请求:/foo/api
案例4
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/;
}
nginx B收到的请求:/foo/api
案例5
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/bar/;
}
nginx B收到的请求:/bar/api
案例6
nginx A配置:
location /foo {
proxy_pass http://192.168.1.56/bar/;
}
nginx B收到的请求:/bar//api
案例7
nginx A配置:
location /foo/ {
proxy_pass http://192.168.1.56/bar;
}
nginx B收到的请求:/barapi
案例8
nginx A配置:
location /foo {
proxy_pass http://192.168.1.56/bar;
}
nginx B收到的请求:/bar/api
看到这里是不是都晕了呢,其实是有规律的
现在把这些案例按表格排列起来,结果表示nginx B收到的请求
表一
案例 | location | proxy_pass | 结果 |
---|---|---|---|
1 | /foo/ | http://192.168.1.48/ | /api |
2 | /foo | http://192.168.1.48/ | //api |
3 | /foo/ | http://192.168.1.48 | /foo/api |
4 | /foo | http://192.168.1.48 | /foo/api |
表二
案例 | location | proxy_pass | 结果 |
---|---|---|---|
5 | /foo/ | http://192.168.1.48/bar/ | /bar/api |
6 | /foo | http://192.168.1.48/bar/ | /bar//api |
7 | /foo/ | http://192.168.1.48/bar | /barapi |
8 | /foo | http://192.168.1.48/bar | /bar/api |
三. 解析
原请求路径:本文中统一为 "/foo/api"
location: 上面表格中的location列
proxy_pass:上面表格中的proxy_pass列
新请求路径:nginx将原请求路径处理过后的字符串
重点对proxy_pass
进行分析,可以分为3种形式
然后按照ip:port后是否接了字符串归为2类,"/"也是字符串,因此1归为一类,2、3归为一类,下面对这两类情况进行说明
当 proxy_pass 的 ip:port 后未接字符串的时候,nginx 会将原请求路径原封不动地转交给下一站 nginx,如案例3和4
当 proxy_pass 的 ip:port 后接了字符串的时候,nginx 会将 location 从 原请求路径 中剔除,再将剩余的字符串拼接到 proxy_pass 后生成 新请求路径,然后将 新请求路径 转交给下一站nginx(上面一种情况实际上和这个是一样的,只不过剔除的字符串是空串~~)
举个最让人疑惑的例子:案例7。proxy_pass 的 ip:port 后接了字符串 "/bar",因此将 location:"/foo/" 从 原请求路径:"/foo/api" 中剔除,变为"api",再将"api"拼接到proxy_pass:http://192.168.1.48/bar 后生成了新请求url:"http://192.168.1.48/barapi",因此下一站的nginx收到的请求就是 "/barapi"。
案例6:proxy_pass 的 ip:port 后接了字符串 "/bar/",因此将 location:"/foo" 从 原请求路径 "/foo/api" 中剔除,变为 "/api",再将 "/api" 拼接到proxy_pass:http://192.168.1.48/bar/ 后生成了 新请求路径:"http://192.168.1.48/bar//api",因此下一站的nginx收到的请求就是 /bar//api。
其它的案例都可以以此类推,现在终于搞明白了,再也不用一头雾水。
最新文章
- 洛谷 P1466 集合 Subset Sums Label:DP
- python RecursionError: maximum recursion depth exceeded in comparison错误
- TODO软件工程--如何预算项目的工期
- centos 非可视化查看已安装的redis
- classpath、path、JAVA_HOME的作用
- phpcms v9 首页怎么调取单页内容,描述,标题等
- MyEclipse 10 和 2014 两个版本共存破解 - imsoft.cnblogs
- NSURLConnection ignore unverified certificate error when sending a synchronise request
- bzoj 1064【noi2008】假面舞会
- AS3.0的int uint Number的使用原则
- 并行开发学习随笔1——plinq并行
- Web流量劫持
- 一个初学者的辛酸路程-socket编程-8
- Ubuntu 简单安装 Docker
- linux 下怎样查找一个文件夹在哪个目录下?
- easyUI tree点击文字展开节点
- typescript入门基础
- linux下yum安装redis以及使用
- 第一次跑eureka
- 简单两步 ~ 绕过 ";Paused in debugger";
热门文章
- Ant—使用Ant构建一个简单的Java工程(两)
- ArcGIS Runtime SDK for WPF 初始化
- 1-9 RHEL7-文件权限管理
- ORM 集合
- 在WPF中引用WinForm的控件
- mysql 在不删除数据的时,同时重新更新主键id
- 元素命名空间中的“MvcBuildViews”无效
- MVC4使用SignalR出现$.connection is undefined错误备忘
- Waveform基于JavaScript的开源多声道音乐波形编辑器
- Ring3下无驱动移除winlogon.exe进程ctrl+alt+del,win+u,win+l三个系统热键,非屏蔽热键(子类化SAS 窗口)