Nginx路径匹配规则是什么

其他教程   发布日期:2023年08月08日   浏览次数:412

本篇内容介绍了“Nginx路径匹配规则是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

1.路径配置的分类

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

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

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

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

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

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

2.例子

  1. location /match {
  2. return 200 'Prefix match: will match everything that starting with /match';
  3. }
  4. location ~* /match[0-9] {
  5. return 200 'Case insensitive regex match';
  6. }
  7. location ~ /MATCH[0-9] {
  8. return 200 'Case sensitive regex match';
  9. }
  10. location ^~ /match0 {
  11. return 200 'Preferential match';
  12. }
  13. location = /match {
  14. return 200 'Exact match';
  15. }

/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路径匹配规则是什么的详细内容,更多关于Nginx路径匹配规则是什么的资料请关注九品源码其它相关文章!