CentOS - Nginx Virtual Hosts

Now we have Nginx installed (whether via the package manager or from source) we are in a position to serve multiple domains using Virtual Hosts.

Do note the layout used in this article is explained here - feel free to use the directories of your choice.

Create the layout

In this example we'll be creating two domains, domain1.com and domain2.com

As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Nginx access to this folder as well:

chmod 755 /home/demo

OK, good.

We can now create the basic layout for each domain. In your home directory create a 'public_html' folder:

mkdir /home/demo/public_html

Now for each domain you want to host (I use the examples of domain1.com and domain2.com) create a folder with a standard set of sub-folders:

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


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

That will create the folders public, private, log and backup for each of our domains (domain1.com and domain2.com).


The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.

So for each domain:

nano /home/demo/public_html/domain1.com/public/index.html

Enter something like this into the file:


Repeat the process so you have a similar file for domain2.com (don't forget to change the index.html content so it shows domain2.com and not domain1.com).

Virtual Hosts Layout

If you have been following the articles for the Nginx install, you will have a 'CentOS' style layout (using a conf.d directory to store your configuration files) whether you installed via the package manager or via source.

As such, we'll use that layout from now on when creating the virtual hosts.

Virtual Host

Let's go ahead and edit the virtual file to add domain1.com:

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

Remember to adjust the path according to your install. So installing from source would require:

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

And add the following:

server {

            listen   80;
            server_name  www.domain1.com;
            rewrite ^/(.*) http://domain1.com/$1 permanent;


server {

            listen   80;
            server_name domain1.com;

            access_log /home/demo/public_html/domain1.com/log/access.log;
            error_log /home/demo/public_html/domain1.com/log/error.log;

            location / {

                        root   /home/demo/public_html/domain1.com/public/;
                        index  index.html;



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

You can, of course, have this the other way around if you prefer.

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

It then defines the log locations for easy analysis and finally sets the server root and the index file.

As said, very basic at this stage.


All that is left to enable our site is to reload Nginx:

sudo /etc/init.d/nginx reload


Now when you navigate to your domain:


You will see the equivalent of this:

Nginx domain1.com


Repeat as necessary

All you need to do for your next virtual host (domain2.com in this example) is to repeat the process:

sudo nano /etc/nginx/conf.d/nginx.conf
# Enter the details for domain2.com as per the example shown above

I know I mention it a lot, but do remember to adjust any paths to match your Nginx installation.


Remember we defined custom locations for the domain logs?

Well, let's have a check they are there:

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

Excellent, everything is working as we expected and we have our domain logs in a nice and convenient location.


Setting up virtual hosts with Nginx is a simple process using the virtual.conf file to define our virtual hosts.

Although the example here is quite basic, I hope you can see that getting to grips with Nginx syntax and configurations is not too difficult.

The next article will concentrate on some other settings for use in the virtual hosts file, thus allowing for more control and flexibility for your hosting needs.

Article Comments:

Diego Plentz commented Thu Mar 05 13:33:17 UTC 2009:

Hey guys, just a tip. Instead of creating a new server to handle the redirect from www.foo.com to foo.com, use this:

server { listen 80; server_name *.domain.com;

    if ($host ~* www.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'

... }

coto commented Thu Feb 25 21:35:15 UTC 2010:

why is downloading my code, when I access to my url

dimpy shah commented Mon Nov 22 10:08:43 UTC 2010:

this site is really helpful to me.i swear i was using nginx for first time . i configured whole nginx as web server . nodoubt i take help of other sites too but still major help was obtained from this content

abhishek kaushik commented Thu Jan 13 03:31:45 UTC 2011:

I am getting a 403 Forbidden error, what could be problem?

abhishek kaushik commented Thu Jan 13 03:37:56 UTC 2011:

I am getting a 403 Forbidden error, what could be problem?

I managed to solve it, by giving permission to /home/demo as o+rx

so that command was: chmod o+rx /home/demo

Thanks for the really nice article.:)

woody commented Thu Feb 17 07:37:36 UTC 2011:

I config follow you but I can't join to www.domain1.com


Humza Bobat commented Mon Feb 21 18:49:18 UTC 2011:

@coto It looks like you have made a mistake in the root directory syntax, add a trailing "/" at the end of the path.

A simple example is below: server { listen 80; server_name site-vhost.com www.site-vhost.com; access_log /home/vhostpath/vhostpath/log/access.log; error_log /home/vhostpath/vhostpath/log/error.log; root /home/vhostpath/vhostpath/; include php; index index.php default.php index.html index.htm; }

Eugene commented Sun Jan 08 22:44:02 UTC 2012:

why i can't configure my domain, i get "welcome to nginx page" all the time

i have kloxo hostinabox which comes with lighttpd, i installed and configured nginx as shown on this website, i stop lighttpd and start nginx but nothing although i followed all steps on this website

thank you

Jered commented Mon Jan 09 21:17:06 UTC 2012:

Sounds like you might benefit from working with a tech, through email or in our live chat, Eugene. Usually when I see the default page in nginx it's because either there's an issue with the way the virtual host is set up, or because you're accessing the site with a domain name or IP address that isn't assigned to a virtual host. For example, if you set up a name-based virtual host for "example.com" and then try going to the site with just its IP address, then you'll get the nginx default page instead of the virtual host.

Robert C. commented Sat Jul 06 03:33:43 UTC 2013:

Thanks Bud. You can't find your virtual host if it doesn't know what IP it should go to. Please include this info for noobs, like me. I manage to figure this out and add it to my /etc/hosts files, but not before a few minutes of head scratching. This should be done in the DNS, but I didn't even fire mine up - even he caching.

submit photos for mon commented Wed Jul 16 18:54:12 UTC 2014:

This page definitely has all of the information and facts I needed about this subject and didn't know who to ask.

Want to comment?

(not made public)


(use plain text or Markdown syntax)