nginx choosing wrong default site

Slashterix asked:

Firstly, I know i can specify default_server to force a default site, but I want to understand why nginx isn’t simply picking the first defined server as documented.

At the end of the http section in nginx.conf I have

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

/etc/nginx/conf.d/*.conf is just some default proxy config and ssl config

Sites defined are

000-default
example.com
zombo.com

the default site points to a local directory and index.html, the other two point at proxied servers.

000-default

server {
  listen   80;
  server_name  aaa-test;

  access_log  /var/log/nginx/localhost.access.log;

  location / {
    root   /var/www/nginx-default;
    index  index.html index.htm;
  }
}

example.com

server {

    server_name example.com *.example.com ;
    listen 80;

    location / {
        proxy_pass  http://10.245.0.19;
        proxy_redirect default;
    }

}

zombo.com

server {

    server_name zombo.com *.zombo.com ;
    listen 80;

    location / {
        proxy_pass  http://10.245.0.36;
        proxy_redirect default;
    }

}

But if I brows to the nginx servers IP, I get an answer from example.com. I’ve tried renaming the config files to load them in different orders, and always get example.com as the default. Even if I name its config file zzz.com

The docs say traffic with no Host header should go to the first virtual host, but I can’t seem to make that happen.

If I remove example.com, then traffic goes to the default host, not zombo.com.

I’m really stumped here …

Edit: Info requested by comment

# ls -lU /etc/nginx/sites-enabled
total 0
lrwxrwxrwx 1 root root 38 Oct  3 00:05 example.com -> /etc/nginx/sites-available/example.com
lrwxrwxrwx 1 root root 34 Oct  3 00:05 000-default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 36 Oct  2 22:58 zombo.com -> /etc/nginx/sites-available/zombo.com

My answer:


nginx is looking at the first defined server, but it isn’t the one you think it is.

If you run ls -lU /etc/nginx/sites-enabled you will see the directory listing in the actual order it appears on disk, which is the order in which they will be read. This is quite often not the order you are expecting.

Which, of course, is one reason why you can explicitly define a default_server.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.