How to Alias directories in NGINX

alias directories in nginx

If you have moved your website directory or you want one directory to serve content for another, without using redirects, then use Aliases. Here’s how to alias directories in NGINX.


How to Alias directories in NGINX

It is quite easy to alias directories in NGINX. Here are the steps.


1. Open NGINX config file

Open NGINx config file in a text editor. You will typically find it at one of the following locations depending on your installation:

  • /etc/nginx/conf.d/default.conf
  • /etc/nginx/sites-enabled/default
  • /etc/nginx/nginx.conf


$ sudo vim /etc/nginx/nginx.conf


2. Configure Alias for directories

You can alias directories simply using alias directive. For example, if you want to alias a directory (e.g /app/ as /web-app/), then simply add anĀ alias directive to its location block.

location /app {
alias /var/www/html/web-app


Please note, the alias directive must contain a file path and not a URL. So in the above example, mention the full path starting from web root (e.g /var/www/html) instead of just /web-app.

Also, the alias directive can be placed only within location directive. It will simply replace the path defined in location block with the file path mentioned in alias directive.


3. Reload NGINX web server

Test the config file to ensure there are no errors.

$ sudo nginx -t


If you get no error message, reload NGINX server to apply changes.

$ sudo service nginx reload


That’s it! Now NGINX will automatically serve content at /var/www/html/web-app when users request for /app.

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!