惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - timseng

ai辅助升级前端安全更新(贼快!hh openclaw的token又耗完了,先不冲了,记录下折腾踩坑过程 (如果需要协助安装500,帮忙卸载250[doge]) VS Code我的配置(持续更新) 推广:flowable工作流引擎就是高级版本的数据库而已(一) springboot升级3x导致邮件发送失败:错误1 jakarta.mail.NoSuchProviderException: smtp nice du更好的du分析文件占用工具ncdu 使用MITMProxy转发https请求到本地、保存鉴权给本地请求(二) 前端发布shell脚本 virtualBox环境Ubuntu升级后太卡,转debian很丝滑 使用visjs分析flowable流程数据 浏览器标签页多行显示:使用Floorp浏览器 最先进的跨平台 Firefox 衍生品 开源之光 开源WAF:ModSecurity探究与部署 动态条件实现java mysql备份恢复云端之核心:更新扫描MySQL库里的所有表的UPDATE_TIME,若发生变动就mysqldump 使用MITMProxy转发请求到本地、保存鉴权给本地请求 这款Ubuntu的默认壁纸水母是真漂亮啊 笑死!'m not going to tell you who, but I know someone who recently locked himself out of the system he was using for an article covering iptables by forgetting the SSH rule. itext的PdfPCell中设置行间距失败的问题 Java递归函数计算递归次数出错
收藏:加不加「/」?Nginx location 路径与 proxy_pass 的规律
timseng · 2024-09-23 · via 博客园 - timseng

从一张梗图开始

起源于在 TG 某个频道看到的一张图:

图下面的评价是:Nginx is so hard!

实际上这张图描述的是 nginx location 的路径配置,及 location 代码块中 proxy_pass 的路径关系,属于 nginx 应用中路径转发的知识。例如图中 Case 1 对应的代码块应该为:

location /test1 {
    proxy_pass http://127.0.0.1:8080;
}

其中 127.0.0.1:8080 是运行的一个后端服务。

例如域名为example.com,那么我在域名后加上 Test URL:example.com/test1/abc/test,那么我的后端服务接收到的路径将是:/test1/abc/test

咋一看似乎完全没有规律,其实之前在一些 nginx 实践中,我个人也深受这个问题的困扰。网上许多文章也并没有详细地解释这个问题。

但把这张梗图和一位朋友交流后,发现了其中的规律。

规律

重点在于 proxy_pass URL 的 IP(域名)、端口后是否有加东西。

如果 proxy_pass URL 的 IP、端口后没加东西

例如:proxy_pass 为http://127.0.0.1:8080,属于没加东西的,而http://127.0.0.1:8080/http://127.0.0.1:8080/app1http://127.0.0.1:8080/app1/这些都归为一类,属于有加东西的。

那么,将 nginx 接收到的 URL(即图中的 Test URL),原封不动地直接加到 proxy_pass URL 后面,就成为了后端程序接收到的路径。

图中的 Case 1 3 9 都是这种情况。

当然,前提是 Test URL 需要与 location 后声明的表达式本身匹配。

如果 proxy_pass URL 的 IP、端口后有加东西

即使是加了一个「/」,也叫有加东西。

如果是这种情况,则进行如下操作:

  1. 将 nginx 接收到的 URL(即图中的 Test URL)中删掉 nginx location 的前缀。
  2. 将上一步得到的字符串直接加到 proxy_pass URL 后面。
  3. 上一步得到的字符串 IP、端口后面的部分,就是后端程序接收到的路径。

例如在 Case 2 中:

Test URL 是/test2/abc/test,nginx location 是/test2。那么将 Test URL 中去掉 nginx location 的部分即为:/abc/test。而 proxy_pass URL 为http://127.0.0.1:8080/,直接加到这后面,得到http://127.0.0.1:8080//abc/test。取 IP、端口后面的部分,为://abc/test。这也就是后端程序接收到的路径中会有两个「/」的原因。

可以自行用这个规律套一下后面几个 Case,都能够符合。

Licensed under CC BY-NC-SA 4.0

原文链接 https://blog.skyju.cc/post/nginx-proxy-pass-and-paths/