5
server{
    ..
    server_name some_other_domain_name.com;
    ..
}

I have mapped my domain name to the public IP of my VM via godaddy.

When I enter the domain name in the browser, then it is able to access the website hosted on the VM (via nginx). However, I was expecting that the request will not be allowed by nginx because the server_name property is set to some_other_domain_name.com

Does nginx not check the server_name property?

variable
  • 289
  • 1
  • 4
  • 15

2 Answers2

7

There is a good explanation of how nginx chooses server and location blocks to proceed request on Digital Ocean Understanding Nginx Server and Location Block Selection Algorithms.

In short, nginx first choose the best match(es) based on listen directives. And checks server_name only if there is more that one match. In that case, if there is no server_name match, then it will choose default block. Default block is either declared as default_server in listen directive, or the first one.

Alexey Ten
  • 9,247
0

You haven't shared your full nginx configuration, so this is a guess of what is missing in the configuration.

nginx always serves something for every request. If there is no server block that has a matching server_name for the request, nginx uses the default server block.

A default server block can be the block where listen directive has default_server modifier.

If no such block exists, the first server block is the default.

In your case, I think you need to set up a default server block like this:

server {
    listen 80 default_server;
    return 444; # breaks connection. Can be 404 if you want to return HTTP 404 not found
}
Tero Kilkanen
  • 38,887