Nginx location中斜线使用说明

一、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/;
}

实际访问的端口服务:http://127.0.0.1:8080/api/abc

B.配置

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

实际访问的端口服务:http://127.0.0.1:8080//api/abc

C.配置

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

实际访问的端口服务:http://127.0.0.1:8080/day06api/api/abc

D.配置

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

实际访问的端口服务:http://127.0.0.1:8080/day06api/api/abc

E.配置

location /day06api/ {
   proxy_pass http://127.0.0.1:8080/server/;
}

实际访问的端口服务:http://127.0.0.1:8080/server/api/abc

F.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080/server/;
}

实际访问的端口服务:http://127.0.0.1:8080/server//api/abc

G.配置

ocation /day06api/ {
   proxy_pass http://127.0.0.1:8080/server;
}

实际访问的端口服务:http://127.0.0.1:8080/serverapi/abc

H.配置

location /day06api {
   proxy_pass http://127.0.0.1:8080/server;
}

实际访问的端口服务: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;
}

实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png

B配置:

location /hello/{
	root   E:/project;
	index  index.html;
}

实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png

C配置:

location /hello{
	root   E:/project/;
	index  index.html;
}

实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png

D配置:

location /hello{
	root   E:/project;
	index  index.html;
}

实际请求资源路径
E:/project/hello/index.html
E:/project/hello/img/123.png

E配置:

location /hello/{
	alias   E:/project/;
}

实际请求资源路径
E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常

F配置:

location /hello{
	alias   E:/project/;
}

实际请求资源路径
E:/project/hello/index.html 404
E:/project/hello/img/123.png 正常

慢慢比较发现规律:

  1. alias指定的目录是准确的,即location匹配访问的path目录下的文件直接是在alias目录下查找的;
  2. root指定的目录是location匹配访问的path目录的上一级目录,这个path目录一定要是真实存在root指定目录下的;
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginxlocation指令用于定义URI匹配规则,用于指定不同的配置块应用于不同的URI。 优先级表示了location配置块的匹配顺序,当请求到达nginx服务器时,nginx会依次遍历配置文件location配置块,然后使用第一个与请求URI匹配的location块进行处理。 具体来说,nginxlocation指令有两种形式:精确匹配和正则表达式匹配。 1. 精确匹配: location = /path { // 配置内容 } 这种形式表示对URI进行完全匹配,只有当请求的URI与指定的path完全相同时才会被匹配。 2. 前缀匹配: location /path { // 配置内容 } 这种形式表示对URI进行前缀匹配,只要请求的URI以指定的path开头就会被匹配到。 3. 正则表达式匹配: location ~* \.(jpg|jpeg|png)$ { // 配置内容 } 这种形式表示使用正则表达式进行URI匹配,只有当请求的URI符合指定的正则表达式时才会被匹配。 当有多个location配置块与请求的URI匹配时,nginx会按照以下优先级进行选择: 1. 精确匹配优先级最高,如果有精确匹配的location块与请求的URI完全匹配,就会选择该location块进行处理。 2. 如果没有精确匹配的location块,nginx会按照配置文件location块的顺序从上到下依次匹配前缀匹配和正则表达式匹配的location块,选择第一个匹配的location块进行处理。 因此,当有多个location配置块与请求的URI匹配时,需要根据优先级和匹配规则来设置location配置块的顺序,以确保请求能够被正确地处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值