Ubuntu Gutsy - Nginx Virtual Hosts

Now we have Nginx installed and running, we can configure it 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

Let's 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 we will 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,logs,backup}


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

That will create the folders public, private, logs 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

Remember that the default layout for Nginx on Ubuntu Gutsy is similar to that of Apache.

We will use a sites-available directory and a sites-enabled directory. Please see this nginx layout article for a reminder of the layout.

Virtual Host

Now we have our virtual host layout ready, let's go ahead and create the vhost file for domain1:

sudo nano /etc/nginx/sites-available/domain1.com

Note: The example I'm using here is pretty basic. However, the next article on Nginx virtual hosts will include details of many more settings that are available.

The contents look like this:

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/logs/access.log;
            error_log /home/demo/public_html/domain1.com/logs/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.


Last configuration we need to do is to 'enable' our site.

This is done with a symlink in the sites-enabled directory as follows:

sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/domain1.com

Restart Nginx

All done for domain1.com so restart Nginx:

sudo /etc/init.d/nginx restart


Now when you navigate to your domain:


You will see the equivalent of this:

Nginx domain1.com


Rinse and Repeat

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

So create a virtual host file:

sudo nano /etc/nginx/sites-available/domain2.com

Enter the details as shown above but for domain2.com and then create a symlink in the sites-enabled folder like this:

sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/domain2.com

Restart Nginx:

sudo /etc/init.d/nginx restart

And away you go.


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/logs/
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 sites-available and sites-enabled folders.

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:

David Parker commented Sun Dec 16 23:59:30 UTC 2007:

In one of the follow up articles, will you show how to link a nginx server to a Mongrel cluster (similar to this article): http://articles.slicehost.com/2007/11/29/ubuntu-gutsy-mongrel-clusters-and-surviving-a-reboot

great stuff! Keep up the good work!

Anders commented Thu Jan 17 10:49:06 UTC 2008:

Might wanna use: sudo /etc/init.d/nginx stop sudo /etc/init.d/nginx start

restart did not work for me, but stop/start got it going.

thx for the great tutorial!

Jason commented Tue Apr 01 04:11:26 UTC 2008:

Same thing, restart does not work. Use stop and start.

Andrew commented Mon Oct 27 04:25:34 UTC 2008:

You can send a HUP signal; sudo kill -HUP `cat /var/run/nginx.pid`

Andrew commented Mon Oct 27 04:27:14 UTC 2008:

More info on Config reload: http://wiki.codemongers.com/NginxCommandLine

Want to comment?

(not made public)


(use plain text or Markdown syntax)