Ubuntu Hardy - Nginx virtual host settings

The previous Ubuntu Hardy 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 Hosts files.


Listen

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.

server_name

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;
}

You can also add a simple rewrite rule here to direct users from www.domain1.com to domain1.com:

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

alias

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:

/home/public_html/domain.com/images/dog.png

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

root

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:

/home/public_html/domain.com/img/dog.png

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

error_page

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;

internal

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:

http://domain.com/error/50x_error.html

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 {
    internal;
}

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:

http://domain.com/error/50x_error.html

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!

Summary

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.

PickledOnion.

Article Comments:

Chris commented Sat May 17 04:32:50 UTC 2008:

Sweet article, guys.

I didn't know about the 'internal' error page setting.

Quick question, any clue how to hide the version number of Nginx in the default 404 page?

I'd like to keep from using a custom 404 error page but it looks like it may be the only solution.

Thanks again for a great article!

Brad Wright commented Mon May 26 13:37:32 UTC 2008:

@Chris:

http {
    ....
    server_tokens       off;
}

Chris Duncan commented Mon Jul 14 15:45:57 UTC 2008:

Hi,

Love your articles. Just wanted to add a correction to the rewrite rule that worked for me.

The line -

rewrite ^/(.*) http://domain1.com permanent;

should read -

rewrite ^/(.*) http://domain1.com/$1 permanent;

What was happening to my site was that any legitimate request for a page starting www.domain.com got redirected to the index page. For example www.domain.com/faq.html would be redirected to domain.com

Regards,

Chris

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)