3
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
  • 177
  • 2
  • 10

2 Answers2

5

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
  • 8,435
  • 1
  • 34
  • 36
  • If the server block only listens to 443 (for example), and this is the only server block, then does this mean that any requests on port 80 will not be served? Or will this block at as the default for port 80 as well? – variable Feb 16 '22 at 09:25
  • If the server block listens only to port 443 it will not match at first step and will never be considered as candidate to serve request to port 80. – Alexey Ten Feb 16 '22 at 09:41
  • But when no matching server block is found, then doesn't it use the 1st server block irrespective of what the 1st server block listen's to? – variable Feb 16 '22 at 09:43
  • @variable, nope. Once again, nginx choose default block only in those that has *best* matching `listen` directive. Have you take a look at the article? – Alexey Ten Feb 16 '22 at 09:45
  • Yes mate, but I didn't get that point into my head. – variable Feb 16 '22 at 09:50
  • Oh jeez, thanks for the tip about the port. I was going in circles with the DNS name. – doublespaces Feb 11 '23 at 01:19
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
  • 36,796
  • 3
  • 41
  • 63
  • `If no such block exists, the first server block is the default.` - but if the first server block only listens to 443 (for example), then does this mean that any requests on port 80 will not be served? – variable Feb 16 '22 at 07:46
  • I don't know since all my use cases require listening to both 80 and 443 ports. – Tero Kilkanen Feb 16 '22 at 07:57
  • @variable, there is a good article on DO https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms – Alexey Ten Feb 16 '22 at 09:07