Table of Content
Useful Arguments
The variable $http_host contains the host in the original request, whereas $uri contains the path after the domain or IP. The last two variables $is_args and $args check for any additional arguments in the initial request, and they add them automatically to the proxied request.
$uri vs $request_uri
$uri
is not equivalent to $request_uri
.
The $uri
variable is set to the URI that nginx is currently processing - but it is also subject to normalisation, including:
- Removal of the ? and query string
- Consecutive / characters are replace by a single /
- URL encoded characters are decoded
The value of $request_uri
is always the original URI and is not subject to any of the above normalisations.
Most of the time you would use $uri
, because it is normalised. Using $request_uri
in the wrong place can cause URL encoded characters to become doubly encoded.
Use $request_uri
in a map directive, if you need to match the URI and its query string.
$host vs $http_host
$host
is a variable of the Core module.
This variable may have a different value from $http_host
in such cases:
- when the Host input header is absent or has an empty value,
$host
equals to the value ofserver_name
directive; - when the value of Host contains port number,
$host
doesn’t include that port number. $host’s value is always lowercase since 0.8.17.
$http_host
is also a variable of the same module but you won’t find it with that name because it is defined generically as $http_HEADER
(ref).
The value of the HTTP
request header HEADER
when converted to lowercase and with ‘dashes’ converted to ‘underscores’, e.g. $http_user_agent
, $http_referer
…;
$host
contains “in this order of precedence: host name from the request line, or host name from the ‘Host’ request header field, or the server name matching a request”$http_host
contains the content of the HTTP “Host” header field, if it was present in the request$server_name
contains the server_name of the virtual host which processed the request, as it was defined in the nginx configuration. If a server contains multiple server_names, only the first one will be present in this variable.