Debian Lenny - Nginx Virtual Hosts #1

Now we have Nginx installed (whether via the package manager or from source) and 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

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 that you want to host (I use the examples of and create a folder with a standard set of sub-folders:

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


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

That will create the folders public, private, log and backup for each of our domains ( and


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

So for each domain:

nano /home/demo/public_html/

Enter something like this into the file:


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

Virtual Hosts Layout

If you have been following the articles for the Nginx install, you will have a 'Debian' style layout (using 'sites-available' and 'sites-enabled' folders) 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 create the vhost file for domain1:

sudo nano /etc/nginx/sites-available/

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

sudo nano /usr/local/nginx/sites-available/

The contents look like this:

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;



Note: This example vhost is pretty basic. However, the next article on Nginx virtual hosts will include more details on some of the settings that are available.

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

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

The second server 'block' 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, it's a very basic config at this stage.


The last configuration step 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/ /etc/nginx/sites-enabled/

Again, depending on how you installed Nginx, you may need to adjust the paths:

sudo ln -s /usr/local/nginx/sites-available/ /usr/local/nginx/sites-enabled/

Restart Nginx

Although there is a restart option for the Nginx init script, it doesn't always work as expected and may not facilitate any changes you have made.

As such, I recommend a stop-and-start approach rather than a simple restart:

sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start


To test the domain without creating a DNS zone and records on some Internet namservers, I've modified the '/etc/hosts' file on my local computer to include some entries mapping '', etc. to the demo slice's public IP:    localhost

# entries related to the demo slice

You can add similar entries in your own 'hosts' file, though it's location will vary depending on what OS is loaded on your local computer (try a Google search.)

NOTE: entries in the 'hosts' file will need to be removed prior to testing and using live DNS zones and records created on Internet nameservers. Failure to remove them will likely lead to confusion later and inaccurate tests of new or modified public DNS records.

With such changes made for testing purposes, you can navigate to your site in a web browser on your local computer:



Rinse and Repeat

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

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

So create a virtual host file:

sudo nano /etc/nginx/sites-available/

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

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

Restart Nginx:

sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx start

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/
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.

Ben B

Article Comments:

Beau commented Mon Feb 01 00:16:24 UTC 2010:

Thanks for this article. I'm trying to switch from apache and this information doesn't seem to be anywhere else. It worked perfectly!

Want to comment?

(not made public)


(use plain text or Markdown syntax)