• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to change Default Document root of Nginx Webserver

1466

How to change Default Document root of Nginx Webserver

The Default document root is the default directory path where the user need to place index.html file to server the web page content for any site. It is imperative for the files to be saved in the appropriate directory for a website to function properly. This tutorial covers the configuration procedure of changing the default document root in Nginx server.

Configuration procedure

To start the configuration procedure, install the Nginx by enabling the epel repository by executing the following command.

[root@node2 ~]# yum install epel-release -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: epel.mirror.angkasa.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Package epel-release-6-8.noarch already installed and latest version
Nothing to do
[root@node2 ~]# yum remove epel-release -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Remove Process
Resolving Dependencies
-->  Running transaction check
--->  Package epel-release.noarch 0:6-8 will be erased
-->  Finished Dependency Resolution
.
.
.
Installed size: 22 k
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing    : epel-release-6-8.noarch                                                                                                                                  1/1
  Verifying  : epel-release-6-8.noarch                                                                                                                                  1/1
Removed:
  epel-release.noarch 0:6-8                                                                                                                                                 
Complete!

Now install the Nginx server by running the following command and press y to continue with the installation process.

[root@node2 ~]# yum install nginx -y
Loaded plugins: fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: epel.mirror.angkasa.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package nginx.x86_64 0:1.10.2-1.el6 will be installed
-->  Processing Dependency: nginx-filesystem = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
-->  Processing Dependency: nginx-all-modules = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
-->  Processing Dependency: nginx-filesystem for package: nginx-1.10.2-1.el6.x86_64
-->  Running transaction check
--->  Package nginx-all-modules.noarch 0:1.10.2-1.el6 will be installed
-->  Processing Dependency: nginx-mod-stream = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
-->  Processing Dependency: nginx-mod-mail = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
.
.
.
nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6               
  nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6           nginx-mod-http-perl.x86_64 0:1.10.2-1.el6           nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6         
  nginx-mod-mail.x86_64 0:1.10.2-1.el6                        nginx-mod-stream.x86_64 0:1.10.2-1.el6            
Complete!

The installation is done successfully. Then execute the following commands to start and enable Nginx service and then allow port 80 for HTTP service in iptables firewall.

[root@node2 ~]# service nginx start
Starting nginx:                                            [  OK  ]
[root@node2 ~]# chkconfig nginx on
[root@node2 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@node2 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@node2 ~]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
iptables: Applying firewall rules:                         [  OK  ]

Now create the new directory for new document root and set proper SELINUX context for Nginx.

[root@node2 ~]# mkdir /myroot
[root@node2 ~]# chcon -Rt httpd_sys_content_t /myroot/

Place the default index.html file to the server content for the specified web page by using the vim editor.

[root@node2 ~]# vim /myroot/index.html
This is new documentroot

Next edit the Nginx default.conf file to mention about the new document root content by running the following command.

[root@node2 ~]# vim /etc/nginx/conf.d/default.conf

Here comment out the default document root line and add the new document root line in the default.conf file.

root /myroot 

Now restart the Nginx service by executing the following service.

[root@node2 ~]# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]

Next open the web browser and check the output by typing the Nginx IP address on the browser.
document root

Wasn' t that an easy configuration process? The document root can put each site’ s document root on its own volume allows the user to scale in response to the needs of a specific site or client.

Tags:
grayson
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

How do I go about making the default file to be "active" (to handle the requests)?

A

You need to "enable" the default file by symlinking it to the sites-enabled folder.

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

Q

Can I use NGINX Plus as an alternative to HAProxy?

A

It is certainly possible to use NGINX Plus as an alternative to HAProxy for HTTP load balancing. NGINX Plus is a full fledged web acceleration solution that includes HTTP request routing, HTT

Q

How is NGINX Plus different from NGINX Open Source?

A

NGINX Plus includes exclusive features not available in NGINX Open Source and award-winning support. Exclusive features includes active health checks, session persistence, JWT authentication,

Q

why we need to change Default Document root of Nginx Webserver?

A

The Default document root is the default directory path where the user need to place index.html file to server the web page content for any site. It is imperative for the files to be saved in the appropriate directory for a website to function properly.

Q

where to check the output of webpage after restarting the Nginx?

A

check the output of webpage after restarting the Nginx to open the web browser and check the output by typing the Nginx IP address on the browser.

Related Tutorials in How to change Default Document root of Nginx Webserver

Related Tutorials in How to change Default Document root of Nginx Webserver

How to Configure Nginx as a Reverse Proxy in CentOS
How to Configure Nginx as a Reverse Proxy in CentOS
Nov 26, 2016
How to install Multiple Nginx instances in same Server on CentOS 6
How to install Multiple Nginx instances in same Server on CentOS 6
Oct 26, 2017
How to configure Nginx Load Balancer in CentOS
How to configure Nginx Load Balancer in CentOS
Nov 9, 2016
How to install Nagios with Nginx on CentOS
How to install Nagios with Nginx on CentOS
Aug 1, 2017
How to create virtual host in NGINX (Both Name and IP based)
How to create virtual host in NGINX (Both Name and IP based)
Jul 18, 2016
How to install Nginx from Source Code on CentOS 7
How to install Nginx from Source Code on CentOS 7
Nov 7, 2017
How to change Default Document root of Nginx Webserver
How to change Default Document root of Nginx Webserver
May 8, 2017
How to Change the Default Port Number of Nginx on CentOS 7.6
How to Change the Default Port Number of Nginx on CentOS 7.6
Jun 17, 2019

Related Forums in How to change Default Document root of Nginx Webserver

Related Forums in How to change Default Document root of Nginx Webserver

Nginx
gokulravichandran2 class=
Failed to start A high performance web server and a reverse proxy server
Dec 23, 2018
CentOS
AadrikaAnshu class=
What does fastcgi_params file contain and what is it used for in nginx on CentOS 7.6
Jun 14, 2019
Nginx
AadrikaAnshu class=
A common misconfiguration is that the 'session.save_path' directive is not pointing to a valid directory.
Jun 15, 2019
Nginx
levi class=
php files are downloading not executing in nginx
Apr 21, 2017
Nginx
lucky class=
[emerg] 32915#32915: bind() to 0.0.0.0:8989 failed (13: Permission denied)
Jun 13, 2019
Nginx
rokkotnik class=
CentOS Nginx load balancer two different apps
Mar 15, 2020
Nginx
akshin class=
Cannot see location = /nginx in backend servers
Jan 8, 2020
Nginx
jayden class=
how to prevent access the hidden files in nginx
Sep 18, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Isaiah ?
What is the use of SUID & SGID commands

How to set the special permissions to the files and folders using SUID and SGID commands...

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.