Dump the code
Best ressources to improve your Python Skills

Difference between the root and alias directives

Created 11 months ago
Posted By admin
4min read
The root and alias directives in Nginx are used to specify the location of files that will be served by the web server. While both directives define the base path for serving files, they have different behaviors and use cases.

root directive:

The root directive sets the root directory from which Nginx will serve files for a given location or server block. It is used to construct the full path to the requested file by appending the URI of the request to the specified root directory.

The root directive sets the base directory for serving files but does not alter the URI path. It simply appends the URI to the specified root directory to construct the file path.

alias directive:

The alias directive is used to specify an alternative location for serving files. It can be used to map parts of the URI path to different directories.

The alias directive replaces the part of the URI specified in the location block with the specified directory path. It effectively performs a substitution of the specified part of the URI with the alias path.

Example

Let's consider an example where you want to serve images from a different directory and you want the URL to include the full path. In this case, the alias directive can be useful. Here's an example:

server {
    listen 80;
    server_name example.com;

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

    location / {
        root /var/www/html;
        index index.html;
    }
}
  • Requests to http://example.com/images/cat.jpg would map to the file /var/www/images/cat.jpg.
  • Requests to http://example.com/index.html would map to the file /var/www/html/index.html.

This setup allows you to serve images from a different directory (/var/www/images/) while still maintaining the full URL path structure. The alias directive replaces the /images/ part of the URI with the specified directory path (/var/www/images/).

Key differences:

1. URI path handling:
   - root: The URI path remains unchanged, and the root directory is appended.
   - alias: The specified part of the URI is replaced with the alias path.

2. Trailing slash handling:
   - root: Nginx appends the entire URI path, including any trailing slash.
   - alias: Trailing slashes in the location and alias directives are significant. The trailing slash in the location directive is preserved in the final path, while the trailing slash in the alias directive is used to replace the matched part of the URI.

3. Use cases:
   - root: Used when the directory structure matches the URI path structure.
   - alias: Used when the URI path needs to be mapped to a different directory structure.

In summary, the root directive is used when the directory structure matches the URI path structure, while the alias directive is used when you need to map parts of the URI path to a different directory structure. The choice between them depends on the desired behavior and the structure of your web application or site.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles