Location

Nginx Location is used to match request URIs, and once matched, the request is processed as defined. Loacation official notes

grammar

Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:	—
Context:	server, location
Copy the code

Match the command

Parameter/modifier instructions
empty Location is followed by no parametersStandard URIRepresents prefix match, which means match from the beginning with the URI in the request.
= Exact match, forStandard URINginx stops searching for other matches.
~ Used forRegular URIBefore, indicates that the URI contains a regular expression,Distinguish betweencase
~ * Used forRegular URIBefore, indicates that the URI contains a regular expression,Does not distinguish betweencase
^ ~ Used forStandard URIIs the longest non-regular match and requires that once a match is found, it will be processed immediately and no more matches will be made.
@ @ defines a named location, and @ defines a locaiton name that is used internally, such as in error_page, try_files commands. Its function is similar to goto in programming.
/ Generic match, if there is no other match, any request will be matched

Matching order

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.

If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.

Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates.

Locations are not matched exactly in the order in which they appear in the configuration file, request URIs are matched as follows:

  1. Nginx first tracks the location defined by the prefix character, selects the longest and records it.
  2. If the longest prefix match is used^ ~Modifier, then just use the configuration without checking the re. If it’s an exact match=, the success of accurate matching will immediately stop other types of matching;
  3. =^ ~If no matches are successful, search for regular matches ~ and ~*. If multiple re matches exist at the same time, they are matched in the sequence in the configuration file. If they are matched, other re matches are stopped immediately.
  4. If all re matches fail, the result of ordinary prefix matches (without arguments ^~) temporarily stored in Step 1 is returned

Priority from high to low:

1. location =    # Accurate matching
2. location^ ~   The parameter prefix matches
3. location ~ #Regular matching (case sensitive)4. location ~ * #Regular matching (case insensitive)5. location /a   # Normal prefix match has a lower priority than parameter prefix match.
6. location /    # Anything that does not match successfully will be matched here
Copy the code

Nginx first checks for common URI matches in multiple locations. If there are multiple matches, Nginx first remembers the one with the highest match. The re matches are then checked in order, from top to bottom, and once a match is successful, the check ends and the request is processed using the Location block. If all the re matches fail, the request is processed using the location block that just recorded the highest match for the ordinary URI.

The sample

location  = / {
  The host name cannot be followed by any string
  [ configuration A ]
}
location  / {
  # Since all addresses begin with a /, this rule will match all requests
  # But the re and the longest string will match first
  [ configuration B ]
}
location /documents/ {
  Match any address that starts with /documents/. After matching, continue searching
  This entry will be used only if the following regular expression does not match
  [ configuration C ]
}
location ~ /documents/Abc {
  Match any address that starts with /documents/Abc. After matching, continue searching
  This entry will be used only if the following regular expression does not match
  [ configuration CC ]
}
location^ ~ /images/ {
  # match any address that begins with /images/. After a match, stop searching for the re and use this one.
  [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
  Match all requests ending in GIF, JPG or JPEG
  However, all images requested under /images/ will be processed by config D because ^~ cannot reach this regex
  [ configuration E ]
}
location /images/ {
  The # character matches /images/
  [ configuration F ]
}
location /images/abc {
  The # character matches /images/ ABC
  [ configuration G ]
}
location ~ /images/abc/ {
	/images/ ABC /
	Select * from [config G]. Select * from [config G]
    [ configuration H ]
}
Copy the code
  • / -> config A

Exact exact match, even if /index.html does not match

  • /downloads/download.html -> config B

After B is matched, if there is no match below, B is used

  • /images/1.gif -> configuration D

^ ~ priority

  • /images/abc/def -> config H

The longest match to H, in addition to matching priority according to the above modifier, there is a rule for the longest match. D/F/G matches according to this configuration, but the longest match prevails. If H is removed from the match, G will be matched. If the G rule is removed, the match will be D, that is, the exact match of ^~ with G is higher than the regular but lower than the empty prefix (location /images/ ABC). The request /images/ ABC /def will be matched by G first, and then the search for the regular match will continue.

<! --Locations tried-->
prefix match for location: /
priority prefix match for location: /images/
prefix match for location: /images/
prefix match for location: /images/abc
case sensitive regex match for location: /images/abc/
Copy the code
  • /documents/document.html -> config /documents/document.html -> config

  • /documents/1.jpg -> Configuration E matches to C and matches to E

  • /documents/ abc.jpg -> config CC matches up to C, matches up to CC in regular order, not down to E

Named after the location

The “@” prefix defines a named location. Such a location is not used for regular request processing. but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

location / {
    try_files $uri $uri/ @custom
}
location @custom {
    # ...do something
}
Copy the code

proxy_pass

Proxy_pass belongs to the directive in nginx_HTTP_proxy_module and is used to set up the agent.

grammar

Syntax:	proxy_passURL; Default: - the Context: the location,if in location, limit_except
Copy the code

Sets the protocol and address of a proxied server and an optional URI to which a location should be mapped. As a Protocol, HTTP or HTTPS can be specified.

Proxy_pass Specifies the protocol and address of the proxy server, which can carry the URI.

Two forms of the proxy_pass URL

Whether to carry the URI indicates that the protocol, domain name, and port do not carry more detailed paths.

With the URI

Such as https://test.com/, https://test.com/aa, that is, as long as the domain name followed by the/of this form.

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:

location /proxy/api {
	proxy_pass https://proxy.com/test/v1;
}
Copy the code

Access/proxy/API/aa/bb becomes an actual visit https://proxy.com/test/v1/aa/bb. That is, a location matching to a path will be replaced by an address in the URL.

With no URI

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI

location /proxy/api {
	proxy_pass https://proxy.com;
}
Copy the code

Access/proxy/API/aa/bb becomes an actual visit https://proxy.com/proxy/api/aa/bb. That is, the path that the location matches is passed directly to the proxy.

Other situations

Location is regular or in named Location

Proxy_pass needs to be set without a URI

The rewrite rule is used and used to handle requests

location /name/ {
    rewrite/name/([^/]+) /users? name=The $1 break;
    proxy_pass http://127.0.0.1;
}
Copy the code

In this case, even those with urIs behind proxy_pass will be ignored, and the URI rewritten by rewrite will be passed to the proxy setting (excluding the URI part of the setting).

location /get {
        rewrite ^/get/(.*)/get? path=The $1 break;
        proxy_pass http://3.233.172.144/node;
}
Copy the code

Visit /get/11111 will become http://3.233.172.144/get? path=11111

Variables used in proxy_pass

location /name/ {
    proxy_pass http://nginx.com$request_uri;
}
Copy the code

Replaces the path portion of the location match. Accessing /name/1111 becomes nginx.com/name/1111

Reference documentation

Nginx Proxy official document

Proxy_pass URL reverse proxy pit

Nginx Chinese documentation

Nginx location match tester

Nginx location rule puzzle matching

Segmentfault.com/a/119000002…

www.journaldev.com/26342/nginx…