1024programmer Nginx How to use the location directive in the LinuxNginx configuration file

How to use the location directive in the LinuxNginx configuration file

location

Syntax: location [=|~|~*|^~] /uri/ { … }
default: no

context: server

This directive accepts different structures depending on the URL. You can configure to use regular strings and regular expressions. If using regular expressions, you must use ~*
The prefix selects a case-insensitive match or ~ selects a case-sensitive match.

determine which location
Directives match a specific directive, regular strings are tested first. The regular string matches the beginning of the request and is case-sensitive, the most specific match will be used (see below to understand
nginx
how to determine it). The regular expressions are then tested in the order in the configuration file. Finding the first matching regular expression will stop the search. If no matching regular expression is found, the regular string result is used.

There are two ways to modify this behavior. The first method is to use
“=” prefix, will only perform strict matching. If the query matches, the search will stop and the request will be processed immediately. Example: If “/” requests occur frequently, use
“location = /” will speed up the processing of this request.

The second is to use the ^~ prefix. Using this prefix with a regular string tells nginx not to test the regular expression if the path matches.

And it’s important that NGINX does comparisons without URL encoding, so if you have a URL link ‘/images/%20/test’ ,
Then use “images/ /test” to qualify the location.

To summarize, commands are accepted in the following order:
1. Directives prefixed with = strictly match this query. If found, stop searching.
2. For the remaining regular character strings, the longer one comes first. If the match is prefixed with ^~, the search stops.
3. Regular expressions, according to the order in the configuration file.
4. If the third step produces a match, use this result. Otherwise use the matching result of the second step.

example:

location = / {
# Only match / queries.
[ configuration A ]
}

location / {
# matches any query, since all requests start with /. But regular expression rules and long block rules will be prioritized for query matching.
[ configuration B ]
}

location ^~ /images/ {
# Match any query that starts with /images/ and stop searching. Any regular expressions will not be tested.
[ configuration C ]
}

location ~* \.(gif|jpg|jpeg)$ {
# Matches any request that ends with gif, jpg or jpeg. However all requests to the /images/ directory will use the Configuration
c.
[ configuration D ]
}

Example request:

/ -> configuration A

/documents/document.html -> configuration B

/images/1.gif -> configuration C

/documents/1.jpg -> configuration D

NOTE: Defining these 4 configurations in any order will still result in the same.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-use-the-location-directive-in-the-linuxnginx-configuration-file/

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
首页
微信
电话
搜索