root

The 'root' directive specifies the root directory of the files that NGINX serves for a location or server block.

Syntaxroot path;
Defaultnone
Contexthttp, server, location, if in location
Arguments1

Description

The 'root' directive in NGINX is used to set the file system path from which files will be served by the server or location context. When a request for a resource (e.g., an HTML file or image) arrives, NGINX constructs the final file path by concatenating the argument specified in the 'root' directive with the URI of the request. For example, if the 'root' directive is configured to /var/www/html and a user requests /images/photo.jpg, NGINX will look for the file at /var/www/html/images/photo.jpg. This allows for serving static files from a specific directory structure on the disk.

The directive can be placed in different contexts, including 'http', 'server', 'location', and even within 'if' statements in a location context. When multiple 'root' directives are specified in different contexts, the most specific one will take precedence. It is important to remember that if a request maps to a directory rather than a file, NGINX will append a trailing slash to the URI and will look for an 'index' file (as defined by the 'index' directive) within that directory. Also, the 'root' directive is designed for static content, and therefore dynamic content, such as that provided by scripts or possibly proxied requests, would not utilize this directive to find files.

Config Example

server {
    listen 80;
    server_name example.com;

    root /usr/share/nginx/html;
    index index.html index.htm;

    location /images/ {
        # Specific location
        root /usr/share/nginx/images;
    }
}

The 'root' directive only works for file serving; do not use it for proxy_pass or other dynamic responses.

Pay attention to all active 'root' directives within the hierarchy, as they may conflict or override each other.

Ensure that the specified path has the correct permissions for the NGINX user to read files.

← Back to all directives