location expression type
~ means to perform a regular match, case-sensitive
~* means to perform a regular match, case insensitive
^~ means an ordinary character match. Use prefix matching. If the match is successful, no other locations will be matched.
= Performs an exact match on ordinary characters. That is, an exact match.
@ “@” defines a named location, used when targeting internally, e.g. error_page, try_files
location priority description
The order of location in nginx configuration does not matter much. The type of the positive location expression is related. For expressions of the same type, longer strings will be matched first.
Here are the instructions in order of priority:
First priority: The equal sign type (=) has the highest priority. Once a match is found, no further matches are looked for.
Second priority: ^~ type expression. Once a match is found, no further matches are looked for.
Third priority: The regular expression type (~ ~*) has the second priority. If there are multiple location regular expressions that can match, the one with the longest regular expression is used.
Fourth priority: regular string matching type. Match by prefix.
location priority example
The configuration items are as follows:
location = / {
# Only match requests /
[ configuration A ]
}
location / {
# Matches all requests starting with /. But if there is a longer expression of the same type, the longer expression is chosen. If there is a regular expression to match, then
# Match regular expressions first.
[ configuration B ]
}
location /documents/ {
# Matches all requests starting with /documents/. But if there is a longer expression of the same type, the longer expression is chosen.
#If there is a regular expression to match, the regular expression will be matched first.
[ configuration C ]
}
location ^~ /images/ {
# Match all expressions starting with /images/, if the match is successful, stop matching search. Therefore, even if there is a matching regular expression location,
# will not be used
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
# Match all requests ending in gif jpg jpeg. But requests starting with /images/ will use Configuration D
[ configuration E ]
}
Request matching example
/ -> configuration A
/index.html -> configuration B
/documents/document.html -> configuration C
/images/1.gif -> configuration D
/documents/1.jpg -> configuration E
Note that the above matches are independent of the order defined in the configuration file.