How do I set a default host for nginx?

I'm trying to figure out how to set a default host for my nginx installation. I found this article in the nginx Wiki:

http://wiki.nginx.org/NginxVirtualHostExample#A_Default_Catchall_Virtual_Host

Unfortunately, this doesn’t work. After restarting I get this:

Restarting nginx: nginx: [emerg] unknown directive "http" in /etc/nginx/sites-enabled/catchall:1
nginx: configuration file /etc/nginx/nginx.conf test failed

After removing the http directive I get this:

Restarting nginx: nginx: [emerg] unknown log format "main" in /etc/nginx/sites-enabled/catchall:7
nginx: configuration file /etc/nginx/nginx.conf test failed

I’m on Ubuntu 10.04.3 where I’m using the official nginx PPA. Version 1.0.9 of nginx is running.


Solution 1:

Nginx configurations are based on a series of nested blocks. There should only be one http block. This is typically defined in /etc/nginx/nginx.conf - all the included files (e.g. from sites available) are included into this http block. In order to setup a default/catch-all host, you only need the server block section for your default host. This is accomplished by adding 'default' to the listen directive. Additionally, it is common to use a server_name which will not match any real name - typically an underscore ("_"), but other characters are acceptable as well. If you omit the default parameter, the first server block is treated as the default. (See nginx's guide to server_names for more information)

The default ('original' in this context) nginx configuration may include a default (catch-all) server block in nginx.conf which you can configure.

As for the second error, ensure that 'main' is defined in nginx.conf (a line should start with 'log_format main ...' (and, of course, check your brackets). You can omit the 'access_log' line entirely if you don't want requests to your default server to be logged (or can change the format by defining a new format).