Nginx Location Matching rules

Source: Internet
Author: User
Location Matching command

~ #波浪线表示执行一个正则匹配, Case sensitive
~* #表示执行一个正则匹配, Case insensitive
^~ #^~ represents normal character matching, and if this option matches, matches only that option, and does not match the other options, typically used to match the directory
= #进行普通字符精确匹配
@ # "@" defines a named location, using internal orientation, such as Error_page, Try_files

The priority of location matching (regardless of the order in which location is in the configuration file)
= Exact match will be processed first. If an exact match is found, Nginx stops searching for other matches.
Normal character matching, regular expression rules and long block rules will be matched by precedence and query, that is, if the match needs to be seen there are no regular expression matches and longer matches.
^~ only matches this rule, Nginx stops searching for other matches, otherwise Nginx will continue to process other location instructions.
Finally, the matching manager with "~" and "~*" instructions, if the corresponding match, the Nginx stop searching for other matches, when there is no regular expression or no regular expression is matched, then the most matched verbatim matching instructions will be used.

Location-Priority Official documents

    1. directives with the = prefix that match the query exactly. If found, searching stops.
    2. All remaining directives with conventional strings, longest match first. If This match used the ^~ prefix, searching stops.
    3. Regular expressions, in order of definition in the configuration file.
    4. If #3 yielded a match, that's result is used. Else the match from #2 is used.
    1. The instruction of the = prefix strictly matches this query. If found, stop the search.
    2. All the rest of the regular string, the longest match. If this match uses the ^? prefix, the search stops.
    3. Regular expressions, in the order in which they are defined in the configuration file.
    4. If the 3rd rule produces a match, the result is used. Otherwise, it is like being used from the 2nd rule.

For example

Location  =/{  # matches only '/'.  [Configuration A]}  location/{  # matches any request because all requests start with "/"  # but longer character matches or regular expression matches will match  [configuration B]}location ^~/images/ {  # matches any request starting with/images/and stops matching other location  [configuration C]}location ~* \. (gif|jpg|jpeg) $ {  # matches a request that ends with a gif, JPG, or JPEG.   # but all requests for/images/directories will be handled by [Configuration C].     [Configuration D]}

The above describes the Nginx location matching rules, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.