How To Change the Nginx Web Document Location on Ubuntu 16.04

In this article, we will learn about how to move or change the location of the Nginx Web server documents folder. By default Nginx Web server default location is at /usr/share/nginx/html which is located on the default file system of the Linux. Generally, this is done, based on the website requirement or client requirements.


  • To complete our setup we need the below requirements.
  • Ubuntu 16.04 installed with a used with sudo permission on the machine.
  • Nginx web server installed
  • A mounted drive or the new document located for where we wanted to change the default document location.

Copying the Document root files to the New location

As the Nginx default document root is at /usr/share/nginx/html, if you have an Nginx installed and existing servers are configured we needed to check the sites-enabled folder which is located on the /etc/nginx/sites-enabled, if the location is changed on the existing servers we can search for the sites-enabled folder with the below command

$ grep “root” –R /etc/nginx/sites-enabled
/etc/nginx/sites-enabled/default: root /usr/share/nginx/html;
/etc/nginx/sites-enabled/default: # deny access to .htaccess files, if Apache's document root
/etc/nginx/sites-enabled/default:# root /var/www/demosite;

Moving the site data to the New location

Assuming that we are moving the default site files /var/www/demosite to the newly created volume, which is located at /mnt/newdatavolume.

$ sudo rsync –av /usr/share/nginx/html /mnt/newdatavolume

Changing the Configuration Files for Nginx

Nginx allow us to change the configuration for the site globally or site specific, in this demo we are using the existing site to be changed from the default location to the new location we have to find the location using the grep command and change the configuration files.

$ sudo vi /etc/nginx/sites-enabled/000-default

We needed to look for the line ‘root’ and update the new location comment that line and add the new line

root /mnt/newdatavolume

Below is the sample file of the default nginx configuration.

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
root /mnt/newdatavolume
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html

Restarting the Nginx Web Server

Once the configuration is changed, then accordingly we needed to restart the Nginx Web server to apply the changes. First, we will check the configuration files and restart the Niginx Web server.

$ sudo nginx –t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx

In the above article and setup we have learned to change the Nginx default document root folder location to the new location mounted on the other volume, where we wanted to manage the sites and bulk site date on a single server.

Updated on: 23-Jan-2020

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started