Linux nginx Config location syntax regular expression

Source: Internet
Author: User

Location

Syntax: Location [=|~|~*|^~]/uri/{...}
Default: No

Context: Server

This command accepts different structures depending on the URL. You can configure the use of regular strings and regular expressions. If you use a regular expression, you must use the ~* prefix to select a case-insensitive match or ~ To select a match that matches the casing.

Determine which location directive matches a specific instruction, the first test of a regular string. The general string matches the beginning of the request and is case-sensitive, and the most explicit match will be used (see below for an understanding of how nginx determines it). The regular expression is then tested in the order of the configuration file. A regular expression that finds the first histograms stops the search. If no matching regular expression is found, the result of the regular string is used.

There are two ways of modifying this behavior. The first method is to use the "=" prefix, which will only perform a strict match. If the query matches, then the search stops and the request is processed immediately. Example: If a "/" request occurs frequently, then using "location =/" will expedite processing of this request.

The second one is to use the ^~ prefix. If you use this prefix for a regular string then tell Nginx if the path matches then do not test the regular expression.

And it is important that NGINX does not have URL encoding, so if you have a URL link '/images/%20/test ', then use "images//test" to limit location.

Summary, instructions are accepted in the following order:
1. = prefix instruction strictly matches this query. If found, stop the search.
2. The remainder of the regular string, long in front. If this match uses the ^~ prefix, the search stops.
3. Regular expressions, according to the order in the configuration file.
4. If the third step produces a match, the result is used. Otherwise, the matching result of the second step is used.

Example:

Location =/{
# Match/Query only.
[Configuration A]
}

Location/{
# matches any query, because all requests have/begin. However, regular expression rules and long block rules will be matched by precedence and query.
[Configuration B]
}

Location ^~/images/{
# matches any query that has/images/start and stops the search. Any regular expression will not be tested.
[Configuration C]
}

Location ~* \. (Gif|jpg|jpeg) $ {
# matches any request that has a GIF, JPG, or JPEG end. However, all requests for the/images/directory will use Configuration C.
[Configuration D]
}

Example Request:

/-Configuration A

Configuration B/documents/document.html

/images/1.gif, Configuration C

/documents/1.jpg, Configuration D

Note: Defining these 4 configuration results in any order will still be the same.

Blog reprint: http://blog.csdn.net/lifaming15/article/details/8461485

Blog reference: http://blog.csdn.net/gzh0222/article/details/7845981

Linux nginx Config location syntax regular expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.