$request_filename

The $request_filename variable contains the full path to the file being requested by the client. — NGINX Core (HTTP)

$request_filename NGINX Core (HTTP)

Description

The $request_filename variable in NGINX is set during the processing of a request and provides the complete filesystem path to the requested resource. It is constructed based on the request URI and the specified root or alias directives in the configuration. When a request is made, NGINX combines the root path defined in the relevant context with the URI, resulting in the absolute path on the server where the requested file or directory can be found. For example, if a request is made for '/images/logo.png' and the server block’s root is set to '/var/www/html', then $request_filename would be '/var/www/html/images/logo.png'. This variable is particularly useful for modules that require file access, such as handling static files, enabling custom logging formats, or implementing conditional logic with the `try_files` directive. $request_filename gets re-evaluated for each request context, ensuring it reflects the current request’s target accurately. Common use cases include logging the requested file path or checking its existence before performing further processing with `try_files` or other configuration directives.

Config Example

location /images/ {
    root /var/www/html;
    if (!-f $request_filename) {
        return 404;
    }
}

Subsystem

http

Cacheable

No

Contexts

http, server, location, if

Ensure that the root or alias is correctly set to avoid incorrect path resolution.

Do not confuse $request_filename with $document_root, as the latter only returns the root path without appending the request URI.

Using $request_filename in a rewrite directive might yield unexpected results if not properly managed with respect to recursion.