Nginx location configuration in the server _nginx

Source: Internet
Author: User
Tags modifier regular expression nginx server

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

Rules
=: Indicates exact URI matching (interested students can look at the difference between the URL and the URI)
~: Indicates a case-sensitive regular match
~*: Indicates a case-insensitive matching
!~ &&!~*: Indicates a regular and case-insensitive mismatch of case-sensitive mismatches
/: Universal match, any request will match to

Location Match target
the location matching test uses only the part of the request URI, not the parameter part. (Reason: Too many arguments to match accurately)

Location Match Order
multiple location configuration under the premise of location matching order (not validated, hey, Google search)
1. First match =
2. Next Match ^~
3. Second, follow the sequence of the configuration file to match,
4. Finally, give/make a general match
Attention:
When a match is successful, stop the match immediately and process the request according to the current matching rule

Demo Example

The Nginx configuration file is divided into three distinct layers from bottom to top:
| HTTP block the protocol level
| Server block the server level
V location block the requested URI

Nginx allows the user to define the Location block and to specify a matching pattern to match a particular URI. In addition to simple strings (such as file system paths), more complex matching patterns (pattern) are also allowed.
The basic syntactic forms of Location block are:

  Location [=|~|~*|^~|@] pattern {...}

[=|~|~*|^~|@] is called location modifier, which defines how Nginx matches subsequent pattern and the most basic properties of the pattern (simple string or regular expression).

About location modifier

1. =

This will exactly match the specified pattern, and the pattern here is limited to a simple string, which means that regular expressions cannot be used here.
Example:

server {
  server_name jb51.net;
  Location =/ABCD {
  [...]
  }
}

Match case:
HTTP://JB51.NET/ABCD # exactly matches exactly
Http://jb51.net/ABCD # If the system running Nginx server is inherently insensitive to capitalization, such as Windows, it also matches
HTTP://JB51.NET/ABCD?PARAM1?M2 # Ignore query string arguments (query string arguments), this is/abcd behind? param1?m2
Http://jb51.net/abcd/# does not match because there is a backslash (trailing slash) at the end, Nginx does not think that this is an exact match
HTTP://JB51.NET/ABCDE # does not match because it is not exactly matched

2. (None)
can not write location modifier, Nginx can still go to match pattern. In this case, match the URI that starts with the specified Patern, note that the URI here can only be a normal string and cannot use regular expressions.
Example:

server {
  server_name website.com;
  LOCATION/ABCD {
  [...]
  }
}

Match case:
HTTP://JB51.NET/ABCD # exactly matches exactly
Http://jb51.net/ABCD # If the system running Nginx server is inherently insensitive to capitalization, such as Windows, it also matches
HTTP://JB51.NET/ABCD?PARAM1?M2 # Ignore query string arguments (query string arguments), this is/abcd behind? param1?m2
A backslash (trailing slash) at the end of the http://jb51.net/abcd/# is also within the matching range
HTTP://JB51.NET/ABCDE # still matches, because the URI begins with a pattern

3. ~
This location modifier is sensitive to case, and pattern must be regular expression

Example:
server {
  server_name jb51.net;
  Location ~ ^/abcd$ {
  [...]
  }
}

Match case:
HTTP://JB51.NET/ABCD # Perfect Match
Http://jb51.net/ABCD # does not match, ~ is sensitive to case
HTTP://JB51.NET/ABCD?PARAM1?M2 # Ignore query string arguments (query string arguments), this is/abcd behind? param1?m2
Http://jb51.net/abcd/# does not match because there is a backslash (trailing slash) at the end and does not match the regular expression ^/abcd$
HTTP://JB51.NET/ABCDE # does not match regular expression ^/abcd$
Note: For some systems that are not case sensitive, such as Windows, ~ and ~*, this is primarily the cause of the operating system.

4. ~*
similar to ~, but this location modifier is case-insensitive, pattern must be regular expression
Example:

server {
  server_name website.com;
  Location ~* ^/abcd$ {
  [...]
  }
}

Match case:
HTTP://JB51.NET/ABCD # Perfect Match
Http://jb51.net/ABCD # Match, that's the case-insensitive feature
HTTP://JB51.NET/ABCD?PARAM1?M2 # Ignore query string arguments (query string arguments), this is/abcd behind? param1?m2
Http://jb51.net/abcd/# does not match because there is a backslash (trailing slash) at the end and does not match the regular expression ^/abcd$
HTTP://JB51.NET/ABCDE # does not match regular expression ^/abcd$

5. ^~
the match situation is similar to 2. (None), the URI that begins with the specified match pattern is matched, and, if the match succeeds, then Nginx stops looking for other Location blocks to match (Location matching order)

6. @
is used to define a Location block that cannot be accessed by an external Client and can only be accessed by Nginx internal configuration directives, such as Try_files or error_page

Demo Example

The

produces the following effect:
access to the root directory/, matching to location/
access to other than hello.php PHP programs, matching to location ~ \.php$, and run with PHP5-FPM
Access to hello.php, matching to location =/hello.php, access redirected to good contact official website

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.