NGINX Root vs Alias


nginx root vs alias

NGINX is one of the most powerful web server used by millions of websites all over the world. It supports numerous server configuration blocks and directives to help you manage simple to complex websites. Root and Alias are two such directives used frequently in almost every website’s NGINX configuration. Let us take a look at the detailed comparison of NGINX root vs alias.

 

NGINX Root vs Alias

Here’s a detailed comparisons NGINX root vs alias.

 

Root Directive

Here’s a sample configuration to understand root directive.


server {
 server_name example.com;
 listen 80;

 index index.html;
 root /var/www/example.com;

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

 location ^~ /images {
  root /var/www/static;
  try_files $uri $uri/ =404;
 }
}

 

root directive is used to indicate the root directory where NGINX must look at to serve the requested URL. It can be used in server or location block. When used inside server block it is applicable for all URLs. When used inside location block it is applicable for only that URL. If there is not root placed in a location block, it will take the root location of its parent server or location block.

In the above case, NGINX will look into /var/www/example.com to serve all URLs such as http://example.com/contact.php

However, NGINX will look inside /var/www/static directory to server URLs in /images/ such as /images/logo.png and /images/graph.png.

So when a user enters http://www.example.com/images/logo.jpg NGINX will look for the file at /var/www/static/images/logo.jpg to be displayed.

So Nginx will map the request made to:

  • http://example.com/images/logo.jpg into the file path /var/www/static/images/logo.jpg.
  • http://example.com/contact-us.html into the file path /var/www/example.com/contact-us.html

 

Alias Directive

Alias, as the name suggests, is another way of referencing a URL or directory on your website. Here’s a sample configuration.


server {
 server_name example.com;
 listen 80;

 index index.html;
 root /var/www/example.com;

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

 location ^~ /images {
  alias /var/www/static;
  try_files $uri $uri/ =404;
 }
}

 

Just like root directive, alias directive will also help NGINX look for the right files to serve URLs. However, it can be placed only in location blocks. If a location doesn’t have Alias directive, it will take the value from its parent location block.

In the above example, when users look for /images/logo.jpg, NGINX will look for the file in /var/www/static/ and not /var/www/static/images/logo.jpg as with root directive.

Please note, with Alias, the location part gets dropped and only the alias path is used to form the absolute path.

 

In this case Nginx will map the request made to:

  • http://example.com/images/logo.jpg into the file path /var/www/static/logo.jpg. No /images/ folder is used here.
  • http://example.com/contact-us.html into the file path /var/www/example.com/contact-us.html

 

 

To summarize, with root the url location is appended to the root path to form the final path, whereas with alias the location is replaced by alias path. Also, root can be placed in both server as well as location blocks whereas alias can only be used in location blocks.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!