A concise guide to the Nginx Location directive

  • 2020-05-09 19:53:45
  • OfStack

The Location instruction in Nginx is an important instruction in NginxHttpCoreModule. The Location directive is relatively simple, but it's something you have to learn when configuring Nginx.

The Location directive, which is used to configure the matching URI, URI, or "/uri/" in the syntax, can be a string or a regular expression. However, if you want to use regular expressions, you must specify a prefix.

1. Basic grammar

1.location [=|~|~*|^~|@] /uri/ {... }
If found, stop the search and process the request immediately.
[-] represents case-sensitive matching
[~*] means case-insensitive matching
[^~] means that only strings are matched and regular expressions are not quashed.
[@] specifies a named location, which is generally used for internal redirection requests only.

2. Matching process

The string is matched first, and the exact match is used. Then, the regular expression matching query begins, the search is stopped after the first result is matched, and if the regular expression is not found, the search result for the string is used. If the string and the regular both match, the regular has a higher priority.

3. Configure the instance


location  = / {
 # Matches only to / Directory query .
 [ config A ]
}
location  / {
 # Match with / The initial query, where all the queries match.
 [ config B ]
}
location ^~ /images/ {
 # Match with /images/ Start the query and no longer check the regular expression.
 [ config C ]
}
location ~* \.(gif|jpg|jpeg)$ {
 # Match with gif, jpg, or jpeg The end of the file, but the priority is lower config C .
 [ config D ]
}

4. Global variables

The variable $args # is equal to the argument in the request line.
$content_length # request header Content-length field.
$content_type # request header Content-Type field.
$document_root # currently requests the value specified in the root directive.
$host # requests the host header field, otherwise the server name.
$http_user_agent # client agent information
$http_cookie # client cookie information
The $limit_rate # variable limits the connection rate.
$request_body_file # client requests the temporary file name of the principal information.
The action requested by the $request_method # client is usually GET or POST.
$remote_addr # client IP address.
$remote_port # client port.
$remote_user # has been authenticated by Auth Basic Module.
$request_filename # the file path of the current request, generated by the root or alias directive and the URI request.
$query_string # is the same as $args.
$scheme #HTTP method (e.g. http, https).
$server_protocol # requests the protocol to be used, usually HTTP/1.0 or HTTP/1.1.
$server_addr # server address, which can be determined after one system call.
$server_name # server name.
$server_port # request arrives at the server's port number.
$request_uri # contains the original URI of the request parameter, not the hostname, as in "/foo/ bar.php ? arg = baz ".
$uri # current URI with no request parameters, $uri does not contain the host name, such as "/foo/ bar.html".
$document_uri # is the same as $uri.


Related articles: