Root and Alias Directives

1 min read

Authors
banner

Root and Alias Directives

Root Directive

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

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.

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

server {
    root /var/www/example;

    location / {
        try_files $uri =404;
    }

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

Choose wisely based on your needs!

© 2025 Vijay Rajendran