1.路径配置的分类在nginx中,一共有4种不同的路径配置方法
= - exact match
^~ - preferential match
~ && ~* - regex match
no modifier - prefix match
#路径完全一样则匹配location = path {}#路径开头一样则匹配location ^~ path{}#正则匹配,大小写敏感location ~ path{}#正则匹配,大小写不敏感location ~* path{}#前缀匹配location path{}
如果存在精确匹配,则先执行精确匹配。如不存在,则进入preferential match。之后在进入regex match,先看大小写敏感的规则,再看大小写不敏感的规则.最后进入prefix match.
= --> ^~ --> ~ --> ~* --> no modifier
在每一个同类型的匹配规则中,按照他们出现在配置文件中的先后,一一对比。
2.例子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'
/match2 # => 'case insensitive regex match'
/match1 # => 'case sensitive regex match'
/match-abc # => 'prefix match: matches everything that starting with /match'
以上就是nginx路径匹配规则是什么的详细内容。