Create CentOS NGINX virtual hosts

This article shows you how to create NGINX® virtual hosts that serve
multiple web domains.

Create the layout for each domain

This example creates two domains, and

Because the default permissions only enable you, the demo user, to browse
your home folder, you need to grant NGINX access to this folder as well by
running the following command:

chmod 755 /home/demo

You can now create the basic layout for each domain. In your home directory,
create a public_html folder by running the following command:

mkdir /home/demo/public_html

For each domain that you want to host, create a folder with a standard set of
subfolders, as shown in the following examples:

mkdir -p /home/demo/public_html/{public,private,,backup}

mkdir -p /home/demo/public_html/{public,private,log,backup}

These commands create the folders public, private, log, and backup for
each of your domains.log

Add public content for the website at

This example creates a very simple HTML file in the public folder
that enables you to quickly check that the virtual host works.

Run the following command to add content for

nano /home/demo/public_html/

Enter code that is similar to the following example into the file:



Repeat the process so that you have a similar HTML index file for

Note: Ensure that you change the content in the index.html file to
show and not

Virtual hosts layout

This article uses a CentOS®-style layout (that uses a conf.d directory
to store your configuration files) when creating the virtual hosts. You
might also have this layout if you installed NGINX by using the
package manager or from source.

Add your virtual hosts to the virtual file

Edit the virtual file to add by running the following command:

sudo nano /etc/nginx/conf.d/virtual.conf

Note: Ensure that you adjust the path according to your installation.

If you are installing from source, you need to enter the following path:

sudo nano /usr/local/nginx/conf/conf.d/virtual.conf

Add the following code to the file:

server {

            listen   80;
            rewrite ^/(.*)$1 permanent;


server {

            listen   80;

            access_log /home/demo/public_html/;
            error_log /home/demo/public_html/;

            location / {

                        root   /home/demo/public_html/;
                        index  index.html;



The first server module in the file is a simple rewrite rule that redirects
visitors to from

The second server module has very basic information, including the
server_name, which is the domain name that you want to serve.

Then, the code defines the log locations so that you can easily find them.
Finally, it sets the server root and the index file.

Reload NGINX

To enable your site, reload NGINX by running the following command:

sudo /etc/init.d/nginx reload

View your website

Navigate to your domain by pasting the following URL into your web browser's
navigation bar:

You should see a simple test web page that displays the information in the
index.html file.

Check the logs

In a previous step, you added code to the virtual.conf file that configured the
locations of the logs. Run the following commands to check that there are now
logs in that location:

ls /home/demo/public_html/
access.log  error.log

Repeat for additional domains

Repeat the same process for your next virtual host,