Setting up different documentroot for subdirectory on nginx

Date Posted: 24-09-2017

Nginx is a fastest webserver when compared to apache.  In this post, we are going to explain how to setup different documentroot for subdirectory. The maindomain documentroot will be /var/www/html and subdirectory folder documentroot /var/www/folder.

Prerequisites:

  1. Nginx Webserver. If nginx is not installed, follow the posts.
    1. Ubuntu 16.04
    2. Centos 7

Implementation:

Let us assumes that the domain name is domain.tld.  Incase if we followed previous post then documentroot will be /var/www/html and /var/www/folder will be new directory which we would like to setup for domain.tld/subdirectory.

Create or edit the nginx configuration file. The default file will be /etc/nginx/sites-enabled/default. If your domain has different configuration file then you need to update the corresponding config file.

Add the below content under server section

vi /etc/nginx/sites-enabled/default

location /subdirectory {
    alias /var/www/folder;
}

Verify the syntax of nginx configutation.

nginx -t

Restart the nginx service

systemctl restart nginx

Create a file under /var/www/folder and upload a content to verify.

vi /var/www/folder/index.html

Test content from subdirectory

Access the subdirectory using domain name http://domain.tld/subdirectory/index.html

 

 

Leave a Reply