1024programmer Nginx Priority of nginxlocation in configuration

Priority of nginxlocation in configuration

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.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/priority-of-nginxlocation-in-configuration/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索