首頁  >  文章  >  運維  >  Nginx路徑比對規則是什麼

Nginx路徑比對規則是什麼

王林
王林轉載
2023-05-21 10:37:103436瀏覽

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  # => 'Preferential match'  
/match2  # => 'Preferential match'  
#/match2  # => => ; 'Case insensitive regex match'  

/MATCH1    # => 'Case sensitive regex match'  
/match-abc # => 'Prefix match: matches everything that starting with /match'  ##### #

以上是Nginx路徑比對規則是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除