Using multiple location blocks in NGINX with different root directives

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful web server software that is widely used for its performance, stability, and rich feature set. One of its most powerful features is the ability to define location blocks, which control the processing of requests for different resource URIs on the server. By understanding how to configure multiple location blocks with different root directives, you can serve different content types more efficiently, support applications that rely on specific URI paths, and implement advanced routing rules.

Understanding the root Directive

The root directive in NGINX is used to define the file system path that should serve as the root directory for requests in the specified context. When a request is received, NGINX appends the request URI to the root path and attempts to serve the resource from there.

user www-data;
worker_processes auto;

http {
    server {
        listen 80;
        root /var/www/html;

        location / {
            try_files $uri $uri/ =404;
        }
    }
}

In this basic example, we set the server-wide root to /var/www/html. All requests are served relative to this path unless otherwise specified.

Defining Multiple Location Blocks

Multiple location blocks can be specified within a single server block to match different request URIs and provide custom handling for each.

server {
    listen 80;
    root /var/www/html;

    location / {
        try_files $uri $uri/ =404;
    }

    location /images/ {
        root /var/www;
    }
}

In this configuration, we have two location blocks. The first one serves general requests, while the second serves any request beginning with /images/, and uses a different root path.

Exact and Preferential Matching

NGINX location blocks can be configured to capture URIs through exact or preferential matching. Understanding the difference is crucial to setting up effective configurations.

location = /exact {
    root /var/www/exact;
}

location ^~ /images {
    root /var/www/images;
}

location / {
    root /var/www/html;
}

The = prefix denotes an exact match, while the ^~ prefix denotes a preferential prefix match.

Using Regex in Location Blocks

NGINX also supports regular expressions for URI matching. This allows for very sophisticated routing rules.

location ~* \.(jpg|png|gif)$ {
    root /var/www/images;
}

location / {
    root /var/www/html;
}

In this example, any request ending in .jpg, .png, or .gif will be served from the /var/www/images directory.

Root Inside Location vs Server Context

While it is possible to define different root directories within location blocks, it is often better to use the alias directive for locations that are not meant to be served out of the primary document root.

location /icons/ {
    alias /var/www/icons/;
}

location / {
    root /var/www/html;
}

By using alias, the /icons/ URI maps directly to the specified directory, without appending the URI to the root path.

Advanced Root and Alias Usages

Advanced configurations may use both root and alias directives together with rewrite rules to perform complex request handling.

location /old/ {
    rewrite ^/old/(.*)$ /new/$1 last;
}

location /new/ {
    alias /var/www/newcontent/;
}

This setup rewrites URIs from an old path to a new path, serving content from a different directory.

Testing and Debugging Your Configuration

When configuring multiple location blocks, it’s important to test your configuration to ensure that requests are routed and handled correctly.

server {
    listen 80;
    root /var/www/html;
    error_log /var/log/nginx/error.log debug;

    location / {
        try_files $uri $uri/ =404;
    }

    # Additional location blocks...
}

Logging at the debug level can provide detailed information about how requests are being processed by NGINX.

Conclusion

Understanding how to use multiple location blocks with different root directives is essential for flexible and efficient web server management in NGINX. By tailoring location block directives to specific needs, you can greatly enhance the performance and functionality of your NGINX server implementation.