最新在配置nginx时,意外发现location中目录斜线有和没有的区别,百度了找找发现没有几个人说的清楚,最后找到一个兄弟写的还比较实用,再次谢过(https://blog.csdn.net/ruihaol/article/details/79526749?from=timeline)(https://blog.csdn.net/u010509052/article/details/105455813)。
一、nginx代理后端服务
nginx 服务器及端口 127.0.0.1:80
后端服务:127.0.0.1:8080
测试url:http://127.0.0.1:80/day06api/api/abc
A.配置
nginx配置如下:
location /day06api/ {
proxy_pass http://127.0.0.1:8080/;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/api/abc
B.配置
location /day06api {
proxy_pass http://127.0.0.1:8080/;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080//api/abc
C.配置
location /day06api/ {
proxy_pass http://127.0.0.1:8080;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/day06api/api/abc
D.配置
location /day06api {
proxy_pass http://127.0.0.1:8080;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/day06api/api/abc
E.配置
location /day06api/ {
proxy_pass http://127.0.0.1:8080/server/;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/server/api/abc
F.配置
location /day06api {
proxy_pass http://127.0.0.1:8080/server/;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/server//api/abc
G.配置
location /day06api/ {
proxy_pass http://127.0.0.1:8080/server;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/serverapi/abc
H.配置
location /day06api {
proxy_pass http://127.0.0.1:8080/server;
}
- 1
- 2
- 3
实际访问的端口服务:http://127.0.0.1:8080/server/api/abc
慢慢比较发现规律:
- 1.proxy_pass 最后有斜线时(即端口后只有斜线,例如A和B中的proxy_pass),location 最后有斜线时,最终组成的url:proxy_pass + location最后一个斜线以后的部分
- 2.proxy_pass 最后有斜线时(即端口后只有斜线,例如A和B中的proxy_pass),location 最后无斜线时,最终组成的url:proxy_pass + 斜线 + location后面的所有部分(但不包含location后面的所有部分的第一个斜线) //其实就是比1多个斜线
- 3.proxy_pass 最后无斜线时,location 最后有斜线时,最终组成的url:proxy_pass + location + 请求url中location以后的所有部分(不包含第一个/)
- 4.proxy_pass 最后无斜线时,location 最后无斜线时,最终组成的url:proxy_pass + location + “/” + 请求url中location以后的所有部分(不包含第一个/)
- 5.proxy_pass 最后有斜线时(且已经包含了至少一级目录,例如E和F中的proxy_pass),location 最后有斜线时,最终组成的url:proxy_pass + location以后的所有部分(但不包含第一个/)
- 6.proxy_pass 最后有斜线时(且已经包含了至少一级目录,例如E和F中的proxy_pass),location 最后无斜线时,最终组成的url:proxy_pass + “/” + location以后的所有部分(包含第一个/)
- 7.proxy_pass 最后无斜线时(且包含了至少一级目录,例如G和H中的proxy_pass),location 最后有斜线时,最终组成的url:proxy_pass + location以后的所有部分(不包含第一个/)
- 8.proxy_pass 最后无斜线时(且包含了至少一级目录,例如G和H中的proxy_pass),location 最后无斜线时,最终组成的url:proxy_pass + location以后的所有部分(包含第一个/)
这个真的不好总结,可能总结的有误,可以直接对应上面的例子。
二、nginx代理本地静态资源
nginx 服务器及端口 127.0.0.1:80
后端服务:127.0.0.1:8080
真实的资源路径:
E:/project/hello
E:/project/hello/index.html
E:/project/hello/img/123.png
测试url:
http://127.0.0.1/hello/index.html
http://127.0.0.1/hello/img/123.png
A:
location /hello/{
root E:/project/;
index index.html;
}
- 1
- 2
- 3
- 4
实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png
B:
location /hello/{
root E:/project;
index index.html;
}
- 1
- 2
- 3
- 4
实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png
C:
location /hello{
root E:/project/;
index index.html;
}
- 1
- 2
- 3
- 4
实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png
D:
location /hello{
root E:/project;
index index.html;
}
- 1
- 2
- 3
- 4
实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png
E:
location /hello/{
alias E:/project/;
}
- 1
- 2
- 3
实际请求资源路径
E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常
F:
location /hello{
alias E:/project/;
}
- 1
- 2
- 3
实际请求资源路径
E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常
1)alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
2)root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?