Ubuntu Gutsy - Nginx Virtual Hosts #2

The previous Ubuntu Gutsy 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 some more settings available to us in the Virtual Hosts 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 like this:

server {
    listen   80;
    server_name domain1.com;


location  /img/ {
        alias  /home/public_html/domain.com/images/;

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/domain.com;

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_error.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_error.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 500 error_page example:

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

location    /error/50x_error.html {

This will ensure that the /error/50x_error.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_error.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!


Between this and the first Nginx virtual host article we have covered quite a few of the settings that are available to us.

Of course, there are more configurations available to us and 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.


Article Comments:

Phil R commented Thu Mar 06 23:02:40 UTC 2008:

Wonderfully useful page - thanks for making this information available!

Want to comment?

(not made public)


(use plain text or Markdown syntax)