Home >Operation and Maintenance >Nginx >What are the Nginx path matching rules?

What are the Nginx path matching rules?

王林
王林forward
2023-05-21 10:37:103476browse

1. Classification of path configuration

In nginx, there are 4 different path configuration methods

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

#路径完全一样则匹配
location = path {
}

#路径开头一样则匹配
location ^~ path{
}

#正则匹配,大小写敏感
location ~ path{
}

#正则匹配,大小写不敏感
location ~* path{
}

#前缀匹配
location path{
}

If an exact match exists, the exact match is performed first. If it does not exist, enter the Preferential match. After entering the Regex match, first look at the case-sensitive rules, then the case-insensitive rules. Finally, enter the Prefix match.

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

In each matching rule of the same type, compare them one by one according to the order in which they appear in the configuration file.

2. Example

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'

The above is the detailed content of What are the Nginx path matching rules?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete