Debian Lenny - Nginx Virtual Hosts #2

The previous Debian Lenny Nginx Virtual Hosts article introduced the basics of the setting up virtual hosts.

This allowed us to serve multiple domains from a single IP address and Slice. Now we can look in detail at more of the settings available to us in the Virtual Host files.


server {
    listen   80;

Inside the server module, the 'listen' setting defines the port for that virtual host.

The two most common ports will be port 80 (the standard HTTP port) and port 443 (the standard HTTPS port.)

Of course, you can add any port. For example, you might want to test a site on port 8080.


The usual course of a virtual host file means the next line is the 'server_name'. As such, the beginning of a vhost server module would normally look something like this:

server {
    listen   80;

You can also add a simple rewrite rule here to direct users from to

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


location  /img/ {
        alias  /home/public_html/;

Sets an alias location.

In this instance, the address '/img/dog.png' would return the file located at:


This can make for cleaner image/file url's and means you can 'share' an image directory with another site on the same Slice.


Following on from the 'alias' setting is a similar one where you can set the 'root' of a location:

location  /img/ {
        root /home/public_html/;

This time, a request for '/img/dog.png' would return the file located at:


There is a subtle, but fundamental difference between the 'alias' and 'root' settings.


error_page    404    /error/404.html;

Sets the location for a custom error page. In this example I have an 'error' folder in my public directory where I place my custom error pages.

You can redirect multiple error types to one file as such:

error_page    502 503 504    /error/50x.html;


Following from the error page setting, it is possible to ensure the user only receives the error page on an actual error.

In other words, at the moment, a user can enter the url:

and be served the 50x.html file.

Not very good, especially if the error file contains variables which may help the user in working out why they are receiving the error (such as previous page, IP address, etc.)

We can set the 'internal' tag to ensure specific pages are only served from an internal trigger and not from an external url request.

Let's have look at an example which includes the above 50x error page example:

error_page    502 503 504    /error/50x.html;

location    /error/50x.html {

This will ensure that the /error/50x.html file is only triggered and served via an internal error.

Now if one were to navigate to:

You would receive a 404 (file not found) error instead of directly accessing the 50x.html file.

Much better.

You can also set this for the 404.html page but there is little point in doing that as all it would do is serve you the same 404 page!


Nginx has many Virtual Host settings and there are more available for use.

If you want to delve into the depths of Nginx you should have a look at the main Http Core Modules page on the Nginx site.

However, there should be enough here to get you going with the basics of setting up your vhosts.

Ben B.

Article Comments:

dave commented Tue Nov 15 02:25:20 UTC 2011:

I have vps using lenny and nginx.

  1. How to setup if people acces my vps ip go to default nginx html file and if the people access my domain name, they redirect to /home/user/www?

  2. my vps only have 1 static ip and 1 their isp dns. can I use only 1 my vps dns?

Thanks anyway :)

Want to comment?

(not made public)


(use plain text or Markdown syntax)