Mastering Nginx

(Ron) #1
Chapter 2

[ 35 ]


  • Regular expressions are matched in the order in which they are found in the
    configuration file. The regular expression search is terminated on the first
    match. The most-specific location match is then used for request processing.


The comparison match described here is against decoded URIs; for example,


a "%20" in a URI will match against a " " (space) specified in a location.


A named location may only be used by internally redirected requests.


The following directives are found only within a location:


Table: Location-only directives

Directive Explanation
alias Defines another name for the location, as found on the
filesystem. If the location is specified with a regular
expression, alias should reference captures defined in
that regular expression. alias replaces the part of the URI
matched by the location, such that the rest of the URI not
matched will be searched for in that filesystem location.
Using the alias directive is fragile when moving bits of
the configuration around, so using the root directive is
preferred, unless the URI needs to be modified in order to
find the file.
internal Specifies a location that can only be used for internal
requests (redirects defined in other directives, rewrite
requests, error pages, and so on.)
limit_except Limits a location to the specified HTTP verb(s) (GET also
includes HEAD).

Additionally, a number of directives found in the http section may also be specified
in a location. See Appendix A, Directive Reference, for a complete list.


The try_files directive deserves a special mention here. It may also be used in
a server context, but will most often be found in a location. try_files will do just


that—try files in the order given as parameters; the first match wins. It is often used
to match potential files from a variable, then pass processing to a named location,


as shown in the following example:


location / {

try_files $uri $uri/ @mongrel;

}
Free download pdf