系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > server > anz > 详细页面

Nginx路径匹配规则小结

时间:2023-03-09来源:系统城装机大师作者:佚名

1.路径配置的分类

在nginx中,一共有4种不同的路径配置方法

= - Exact match
^~ - Preferential match
~ && ~* - Regex match
no modifier - Prefix match

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#路径完全一样则匹配
location = path {
}
 
#路径开头一样则匹配
location ^~ path{
}
 
#正则匹配,大小写敏感
location ~ path{
}
 
#正则匹配,大小写不敏感
location ~* path{
}
 
#前缀匹配
location path{
}

上面的执行顺序是,优先查看Exact match,若存在,则停止。如不存在,则进入Preferential match。之后在进入Regex match,先看大小写敏感的规则,再看大小写不敏感的规则.最后进入Prefix match.

= --> ^~ --> ~ --> ~* --> no modifier

在每一个同类型的匹配规则中,按照他们出现在配置文件中的先后,一一对比。

2.例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
location /match
  return 200 'Prefix match: will match everything that starting with /match'

   
location ~* /match[0-9] { 
  return 200 'Case insensitive regex match'

   
location ~ /MATCH[0-9] { 
  return 200 'Case sensitive regex match'

   
location ^~ /match0
  return 200 'Preferential match'

   
location = /match
  return 200 'Exact match'
}

/match     # => 'Exact match'  
/match0    # => 'Preferential match'  
/match1    # => 'Case insensitive regex match'  
/MATCH1    # => 'Case sensitive regex match'  
/match-abc # => 'Prefix match: matches everything that starting with /match'  

到此这篇关于Nginx路径匹配规则小结的文章就介绍到这了

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载