$realpath_root
The $realpath_root variable returns the canonical filename of the root directory of a location block, resolving any symbolic links. — NGINX Core (HTTP)
Description
The $realpath_root variable in NGINX provides the absolute file path to the root directory of the current location block for a given request, following all symbolic links. When a request is processed, NGINX resolves the requested URI against the configured document root, which is defined using the root or alias directives. If the root directory specified is a symbolic link, NGINX will resolve it to its actual physical location on the filesystem, ensuring that the path is accurate and not pointing to litigious symlink references. This variable is particularly useful in configurations where symlinked directories are used for serving files, as it guarantees the correct path is utilized for security checks or file-related operations. It is set during the request processing phase when NGINX examines the configuration block handling the request. Typical values for $realpath_root would include paths like '/var/www/html' or '/usr/share/nginx/html' depending on the user-defined configurations in the respective `server` or `location` contexts.
Config Example
location /images/ {
alias /var/www/data/images/;
try_files $uri $uri/ =404;
}
location = /images/logo.png {
internal;
root /var/www;
error_page 404 = @fallback;
}
# Use of $realpath_root
add_header X-Realpath-Root $realpath_root;Subsystem
httpCacheable
NoContexts
http, server, locationEnsure that the path set in the root or alias directive is valid and exists, otherwise $realpath_root may not return a usable path.
Be cautious of using $realpath_root in conjunction with directory traversals, as it resolves paths that may bypass security checks if not properly configured.