$request_uri

The $request_uri variable contains the original request URI as sent by the client, including the query string if present. — NGINX Core (HTTP)

$request_uri NGINX Core (HTTP)

Description

The $request_uri variable is a core variable in NGINX that captures the complete URI requested by the client. This includes both the path and the query string, if any is provided. It is set during the processing of a request, specifically when NGINX reads the request line from the client, which allows it to create a response based on that URI. This variable is vital for routing requests, generating logs, and creating redirects or rewrites as needed. Typical values of $request_uri could be paths like "/products/item?id=123" or "/api/v1/users", where the former includes a query string while the latter does not. This makes $request_uri a comprehensive way to reference the precise request from the user's perspective. It is often used in conjunction with other variables to log requests, control access, and optimize response behaviors depending on the type or parameters of the request.

Config Example

location /api {
    if ($request_uri ~* "/products") {
        # Handle product requests
        proxy_pass http://backend;
    }
}

location / {
    log_format main '$remote_addr - $remote_user [$time_local] "$request $request_uri" $status $body_bytes_sent';
    access_log /var/log/nginx/access.log main;
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Make sure to use $request_uri only when you need the full URI. If you need just the path without the query string, consider using $uri instead.

Be cautious when using $request_uri in conditional blocks as it may lead to unexpected behaviors if not properly evaluated.