[Translation] nginx part on location

Translation:

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }``location @name { ... }
Default:
Context: serverlocation

Configured according to the request URI.

In order to decode the text "% xx" in the form of a relative path. "" Format and "...", and two or more adjacent slashes after compression to regulate a single slash URI matching is performed.

A location may be defined as a prefix string or a regular expression. Regular Expressions by the prefix " *" (ignoring case) or " " (case sensitive) modified in these positions, the longest prefix string match location will be selected and remembered. Then it will be in the order matches the regular expression regular expressions appear in the configuration file. Terminates on the first regular expression match, and the corresponding configuration will be used. If it does not match the regular expression, remember the location of the previous prefix string matching configuration will be used.

block location can be nested, with some exceptions mentioned below.

For a case-insensitive and operating systems such as macOS Cygwin, prefix string match case is ignored. However, a comparison of bytes to be limited environment.

Regular expressions may comprise capture, captured content can be used in the following other instructions.

If a matching location longest prefix string of "^ ~" modified, the regular expression will not be checked after the.

Use the "=" is modified to define a strict matching URI and location. If strict mode on a match, the search will be terminated. For example, if a request "/" occurs frequently, the definition of "location = /" will process these requests quickly. After the first match on strict location will terminate the search. This location obviously can not contain nested location.

NOTE: In version 0.7.1 to 0.8.41, if a location request matches the prefix string, if not "=" or "^ ~" modified, the search will stop and the regular expression is not checked.

Let us be explained through an example:

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

"/" Will match a request configuration A, "/ index.html" request will match configuration B, "/ documents / document.html" request will match configuration C, "/ images / 1.gif" request will match configuration D, "/ documents / 1.jpg" request will match the configuration E.

"@" Prefix as a prefix defined location. Such a location request is not for a conventional process, but the request for redirection. They can not be nested and can not contain nested position.

If a location by the prefix character is defined with a slash at the end, and the request is proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass grpc_pass or one treatment and then performs special handling. In response to a request string URI no slashes, etc. However, a permanent redirection cod 301 will return the request URI and belt slash. If this is not a strict matching URI and location can be defined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}

important point

  1. The location is defined by a string from the front of the path of the URI start of the match, which is why always emphasized the reasons prefix string of URI's path is not there a part of the match on the line, you must start over from the path of the match, but regular We do not have this limitation.
  2. Do not be backward about not being back in the match ^ ~ match up. Here we must note that the requirements ^ ~ matching is performed when the search string ^ ~ is to be remembered that location, which is the longest prefix match on the location.

reference

nginx documentation locaiton part

发布了48 篇原创文章 · 获赞 52 · 访问量 5万+

Guess you like

Origin blog.csdn.net/letterTiger/article/details/102951725