Matching rules for location in Nginx configuration

Source: Internet
Author: User

Syntax rules for location-matching URLs:

Location [=|~|~*|^~]/uri/{...}

= indicates exact match

~ Indicates a case-sensitive regular match

^~ represents a URL that begins with a regular string;

~* indicates a regular match that does not differentiate between large messages

!~ (*!~) indicates a case-insensitive match

/wildcard, any request can match to this location, if any other location can match, it will overwrite the location

Match order:

1) match normal URL first, match regular

2) the "normal location" matching rule is "maximum prefix", so "normal location" does not have to do with location editing order;

3) The execution logic of the location is basically not related to the order, but in the way of regular matching, the first URL is matched, and the following URL is not continued;

In this case, if there is a match on the normal localtion, there is no regular match, then a normal match is used, and if the maximum prefix match for both normal location and regular match, then the regular match overrides the maximum prefix match.

4) After matching "normal location", sometimes need to continue to match "regular location", and sometimes do not need to continue to match "regular location".

In both cases, you do not need to continue to match the regular location: (1) when the normal location in front of the specified "^~", specifically told Nginx this article general location once the match, you do not need to continue regular match, (2) when the normal location exactly match, not is the maximum prefix match, the matching regular is no longer continued.

For example:

Location  =/{  # matches the query/only.  [Configuration A]} Location  /{  # matches * query, since all queries begin with/, but regular  # expressions and any longer con Ventional blocks would be  # matched first.  [Configuration B]} location/documents/{  # matches any query beginning With/documents/and continues searching,  # so regular expre Ssions'll be checked. This would be matched only if  # Regular expressions T find a match.  [Configuration C]}  Location ^~/images/{  # matches * Query beginning With/images/and halts searching,  # so regular expressions Won't is checked.  [Configuration D]} Location ~* \. (gif|jpg|jpeg) $ {  # matches any request ending in gif, JPG, or JPEG. However, all  # requests to The/images/directory would be a handled by  # Configuration D.     [Configuration E]}

Request example, matching location:

    • /-Configuration A
    • Configuration B/index.html
    • /documents/document.html, Configuration C
    • /images/1.gif, Configuration D
    • /documents/1.jpg, Configuration E

Matching rules for location in Nginx configuration

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.