Summary of Nginx Location directive URI matching rules

  • 2020-05-17 07:36:55
  • OfStack

1, the introduction

The location instruction is the core configuration of the http module. It receives the request sent by the user according to the predefined URL matching rule. According to the matching result, it forwards the request to the background server, directly rejects the illegal request and returns 403, 404, 500 error handling, etc.

2. location instruction syntax

location [=|~|~*|^~|@] /uri/ {... } or location @name {... }

3. URI matching mode

The location instruction is divided into two matching modes:
1 > Normal string matching: a rule that begins with = or without a boot character (~)
2 > Regular matching: begins with ~ or ~* for regular matching, and ~* for regular insensitivity

4. location URI matching rules

When nginx receives a request, it intercepts the URI portion of the request and searches for all the URI matching patterns defined in the location directive. In the server module, multiple location instructions can be defined to match different url requests, and multiple URI matching patterns configured by different location. The general matching principle is: match the normal string pattern first, and then match the regular pattern. Only identify URI parts, such as request for: / test abc/user do & # 63; name = xxxx

After 1 request comes, the process of Nginx matching this request is as follows:

1 > To find if there is a first = at the beginning of an exact match, such as: location = / test abc/user do {... }

2 > Then look for the ordinary match, with the maximum prefix as the principle. If there are the following two location, the last item will be matched
* location /test/ {... }
* location /test/abc {... }

3 > After matching to a normal format, the search does not end, but instead stores the result of the current match and continues to search for the regular match pattern

4 > When the first match is found in location of all regular matching patterns, this is the final match result
So regular match matching rules are affected by the order in which they are defined, but normal match patterns are not

5 > If no regular match is found, the result cached in 3 is the final match
6 > If none of the 1 matches are found, 404 is returned

5. Difference between exact matching and fuzzy matching
location = / {... } and location / {... } differences:
* the first one is an exact match and only responds to/requests. All /xxx or /xxx/xxxx requests are not prefixed to it
* the last one is that any request prefixed with/will be matched. For example: /abc, /test/abc, /test/abc/aaaa

6. Regular and non-regular matching

1 > location ~ /test/.+.jsp${... } : regular matching, support for standard regular expression syntax.
2 > (1)... } : ^~ means to turn off regular matching. When the normal matching pattern is found, the search for regular matching pattern will not continue.


...
http {
  ...
  server {
    listen    80;
    server_name localhost;

    location / {
      root  html;
      index index.html index.htm;
      # deny all;  Reject the request and return 403
      # allow all;  Allow the request 
    }

    location /abc {
      deny all;
    }

    location  ~  /.+\.jsp$ {
      proxy_pass http://location:9090;
    }

    #  Match all /test Under the path of jsp file 
    location ~ /test/.+\.jsp$ {
      proxy_pass http://localhost:8080;
    }

    #  Define the types of error pages 
    error_page 404 /404.html

    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }

    # @ Similar to variable definitions 
    # error_page 403 http://blog.csdn.net; # This definition does not allow for requirements to be exploited @ Define temporary variables to implement 
    error_page 403 @page403;
    location @page403 {
      proxy_pass http://blog.csdn.net;
    } 
  }
}

Related articles: