$uri
The $uri variable in NGINX returns the URI part of a requested resource without the domain or protocol information. — NGINX Core (HTTP)
Description
The $uri variable is set during the processing of an HTTP request in NGINX and is primarily populated with the unescaped request URI, which is the part of the URL following the domain and protocol. This variable can include the path and query string of the requested resource but will not contain any additional server or location directives' modifications. The value of $uri is determined early in the request processing, typically when NGINX parses the client request. This means that it reflects the initial value sent by the client before any internal rewrites or redirects. When used in server configurations, the $uri variable allows you to make decisions based on the actual requested resource. It is especially useful in location blocks for access control, logging, or redirecting traffic. The value is usually a string representing a file path or a route and can be formatted like '/images/photo.jpg' or '/api/v1/users'. It should be noted that if the request was processed under a different location block that rewrote the URI, the $uri variable would reflect that change.
Config Example
location /images {
alias /var/www/images;
error_page 404 = /404.html;
}
location /api/v1 {
proxy_pass http://backend;
access_log /var/log/nginx/api_access.log;
if ($uri ~* /user/(\d+)) {
# Do something specific for user URIs
}
}Subsystem
httpCacheable
NoContexts
http, server, location, if, limit_exceptThe value of $uri can change based on internal rewrites configured in the NGINX configuration, so be cautious when using it to match specific locations.
$uri does not include the query string; use $request_uri if you need both the URI and the query string.