SoFunction
Updated on 2025-03-02

Detailed explanation of root and alias directive examples in Nginx

Nginx is a powerful web server and reverse proxy server that is widely used to provide static file services, load balancing, and as an HTTP cache. When configuring Nginx, root and alias are two important but often confusing instructions, especially for specifying the location of resources in the server file system. This blog post will introduce in detail the uses of these two instructions, their differences, and specific usage scenarios.

root command

The root directive is very common in Nginx configuration and is used to set the root directory of resources in the server. This means that Nginx will look up and serve files from this specified directory.

Example

Suppose the website has a document root directory located in /var/www/html, and needs to provide services for the website root URL. You can set it in the configuration file like this:

server {
    listen 80;
    server_name ;
    root /var/www/html;
    location / {
        try_files $uri $uri/ =404;
    }
}

In this configuration, if you request access to /images/, Nginx will look for the file in /var/www/html/images/.

alias directive

Unlike the root directive, alias is used to set a path alias for a specific location block, which means it allows you to change the path you look for for a specific URI request.

Example

Assuming you want the /images directory to map to the physical path /data/uploads, but don't want to expose it to the URL, you can use alias:

server {
    listen 80;
    server_name ;
    location /images/ {
        alias /data/uploads/;
    }
}

In this configuration, if the client requests /images/, Nginx will actually return the contents of /data/uploads/.

The main differences between root and alias

  • Path stitching method: When using root, the URI specified in the location block will be spliced ​​directly behind the root path. Alias ​​will replace the matching path in the location with the path specified by alias.
  • Applicable scenarios: root is suitable for a wide range of websites and is often defined in server or location blocks. alias is suitable for changing paths to a specific location individually, and is suitable for finer granular path control.

Notice:

  • When using alias, you must add "/" after the directory name.
  • When using regular matching, alias must capture the content to be matched and use it at the specified content.
  • alias can only be located in the location block. (root can not be placed in location)

Use scenarios

  • Using root: When you want to provide a unified root directory for the entire server or a specific location, using root is the easiest and most straightforward way.
  • Use alias: When you need to map specific resources on the server, and these resources are not in the current root directory, alias is indispensable. For example, if some dynamically generated files are stored in a directory different from static files, special processing can be performed through alias.

This is the end of this article about Nginx root and alias instructions. For more information about Nginx root and alias instructions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!