What are the Nginx path matching rules?

10-30-2023

1. Classification of path configuration In nginx, there are four different path configuration methods.

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

# If the paths are exactly the same, they will match location = path {}# If the paths start the same, they will match location ~ path{}# Regular matching, case-sensitive location ~ path {} # Regular matching, and case-insensitive location ~* path{}# Prefix will match location path{}

If there is an exact match, the exact match is performed first. If it does not exist, enter Preferential match. After that, enter Regex match, first look at the case-sensitive rules, then look at the case-insensitive rules, and finally enter 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'

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us