12

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.

2 Answers 2

19

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

3
  • That was fast. Thanks. Gonna try this out tomorrow after sleeping a little bit ;-).
    – Ulf
    Nov 11, 2011 at 0:00
  • Using _ as the server_name doesn't add to the "defaultness" of the server{}, it's just commonly used to keep the catchall from conflicting with other server blocks, so there's no 'override' about the default flag on the listen directive. It's the only thing explicitly marking the server as default. Still, +1
    – kolbyjack
    Nov 11, 2011 at 14:38
  • @kolbyjack - Thanks for the correction - I'll fix it in my answer.
    – cyberx86
    Nov 11, 2011 at 21:07
-1

Try this. Replace return 404 with whatever default action you want.

https://serverfault.com/a/946083/453080

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .