Nginx Responding To All Domain Names

The first server listing in the nginx configuration will be the default action for all domain name requests to that server. This will be true unless a server action is labeled default_server. This means that the server might render a website for any domain name that is requested from nginx.

To prevent this behavior you must specify what the default action will be. I generally want my nginx server to reply with a 404 error when a nonexistent domain name is queried.

One way to do this is to ensure that server { return 404; } is the very first server in your nginx config files. This is easy to do if you have a single config file. Just put the above as the first server listing followed by server listing for your domain names, like this: server { return 404; } server { server_name FIRST_DOMAIN; [...] } server { server_name SECOND_DOMAIN; [...] }

The above works well if you have a single configuration file. Maybe you only have a single file with one or two domain names in your sites-enabled folder.

However, if you have multiple configuration files, it is much easier to have a default file with the default_server option set. This tells nginx that this is the default behavior for all unknown domain requests.

This configuration file would look something like this: server { listen *:80 default_server; return 404; }