nginx is defaulting all requests to, what should be, a vhost

I have nginx setup on my server with the following config file

worker_processes  2;
events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    access_log    logs/mysite.access.log;
    error_log     logs/mysite.error.log;

    gzip            on;
    gzip_min_length 1100;
    gzip_buffers    4 8k;

    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay   on;
    keepalive_timeout  75 20;

    server {
        listen 80;
        server_name website.com www.website.com;

        location / {
            include proxy.conf;
            proxy_pass  http://127.0.0.1:5000;
            proxy_redirect  default;

            if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
                expires max;
                break;
            }
        }
    }

    server {
        listen 443 default ssl;
        server_name website.com www.website.com;

        ssl_certificate     /etc/ssl/localcerts/website.com.crt;
        ssl_certificate_key /etc/ssl/localcerts/website.com.key;

        location / {
            include proxy.conf;
            proxy_pass  http://127.0.0.1:5000;
            proxy_redirect  default;

            if ($request_uri ~* "\.(ico|css|js|gif|jpe?g|png)$") {
                expires max;
                break;
            }
        }
    }
}

That should mean that when I visit 'website.com', it will send the request to 127.0.0.1:5000 and I'll see the website running there, right? The problem is, if I visit the IP address of the server, I see the site running on website.com

Coming from Apache VHosts, this is wrong... Visiting the IP should show me the default nginx HTML file or a 404. Not the website.com virtual host.

What have I done wrong?


Solution 1:

You've assumed Nginx behaves like Apache. :)

http://nginx.org/en/docs/http/server_names.html http://wiki.nginx.org/NginxHttpCoreModule#server_name

Basically. If there are no matching server block it will use the server block with a matching listen directive using the [default|default_server] flag and if none is found it will use the first server block defined.

In your case you have no server blocking matching the IP and you have no server block listed as the default, so it uses the first one defined.