Root and Alias Directives

1 min read

Authors
banner
Table of Contents

Root and Alias Directives

Root Directive

Use root for your main content directory. It appends the request path to the root.

nginx
server {
    root /var/www/example;

    location / {
        # /file.html maps to /var/www/example/file.html
    }
}

Alias Directive

Use alias for special paths. It replaces the location path with the specified directory.

nginx
server {
    location /downloads/ {
        alias /mnt/storage/files/;
        # /downloads/file.zip → /mnt/storage/files/file.zip
    }
}

Key Differences

FeatureRootAlias
Path behaviorAppendsReplaces
Use caseGeneral contentSpecial paths
PerformanceBetterGood

Practical Example

nginx
server {
    root /var/www/example;

    location / {
        try_files $uri =404;
    }

    location /media/ {
        alias /var/media/uploads/;
    }
}

Choose wisely based on your needs!

© 2026 Vijay Rajendranbuilt with Next.js & Tailwind · deployed on Vercel