Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I have an Nginx with a number of enabled server blocks. Each server answers to 1 canonical domain and may forward 1 or more to that canonical URL. I have at least one server (haven't checked all of them yet) where, if I type in a non-existent domain that points to this box, Nginx displays a site of its choosing (always the same site, but not one that I'm after).

I've poked around the config file for the site I always land on, but don't see anything obvious that would identify it as any kind of default site and yet there it is, always showing up when I fat finger a URL.

Any thoughts on what I should be looking for to track this down?

share|improve this question

marked as duplicate by Scott Pack, squillman, mdpc, Nathan C, voretaq7 Oct 10 '13 at 17:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 3 down vote accepted

Add default_server to your listen directive in the server that you want to act as the default.

share|improve this answer
well, that's the thing. i really don't want a default, but I seem to have one. I'd rather have nginx just throw whatever error it throws if what i type doesn't exactly match something. – Rob Wilkerson Jul 19 '13 at 17:36
@RobWilkerson You mean the error it throws when you request a resource that doesn't exist? That's a lot different than a hostname that doesn't match a server, but you can configure it to get the same effect: create a default server block with nothing but the listen with default_server, and try_files =404;. – Shane Madden Jul 19 '13 at 18:01
Let me ask the question a different way. What makes Nginx load site X when a non-matching domain is called? Why site X and not site Y? Why doesn't it just throw an error? – Rob Wilkerson Jul 19 '13 at 19:41
@RobWilkerson It always selects a default server for every IP:port listening pair, and serves it for any request that doesn't match a server block. I didn't write it so I can't tell you the reason for this behavior, but I'd say probably because it's a lot more user friendly and easy to troubleshoot "I got the wrong content" versus "404? Huh?". The behavior you want is very easily achieved, so why is this a problem? – Shane Madden Jul 19 '13 at 19:45
I don't know that it's a problem, per se. Just learning my something new for the day. :-) – Rob Wilkerson Jul 19 '13 at 20:10

Why is nginx doing this?

The reason for this is simple. Very old or broken clients do not send the Host HTTP header field in their requests and if you are using name based server blocks (name based virtual hosts in Apache terms) nginx is not able to determine which of the servers you configured is meant by the client. The same problem is true for any other web server that supports this name based system. This problem would not arise if you would be using an IP based system for each domain (which also means that you have several network interfaces).

More on this topic? How nginx processes a request

Which is the first?

nginx will select the server that comes first if no default flag was set on any listen directive:

server {

server {
} will be default.

If you automatically include the symbolic links from sites-enabled (default config) the file that comes first in the directory will be your first server.

What can I do to prevent this?

Good question and you should prevent it. There is no reason to support these old clients and absolutely no reason to support broken clients. The problem is easily solved by creating a default catch all server config. The following example is from one of my projects and targeted towards the current dev version of nginx (1.5.2 - but should work with older versions as well):

# /etc/nginx/sites-enabled/_.conf

# Default server for clients who do not send correct Host header.
# The underline in the file name makes sure that this file comes first in the dir.
server {
  server_name _;
  listen *:80 default_server deferred;
  return 444;

Configuration is trimmed, more nginx configuration stuff.

share|improve this answer
+1 Great info. Thanks. – Rob Wilkerson Jul 22 '13 at 17:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.