Score:0

nginx is not respecting the server_name value

in flag
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?

Richard Smith avatar
jp flag
See [how Nginx processes a request](http://nginx.org/en/docs/http/request_processing.html).
in flag
So server_name is used only when there is a clash with IP:port combination?
us flag
`server_name` is always used. The issue is likely with your default server block as explained below.
Score:1
br flag

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.

doublespaces avatar
ps flag
Oh jeez, thanks for the tip about the port. I was going in circles with the DNS name.
in flag
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?
br flag
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.
in flag
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?
br flag
@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?
in flag
Yes mate, but I didn't get that point into my head.
Score:0
us flag

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
}
in flag
`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?
us flag
I don't know since all my use cases require listening to both 80 and 443 ports.
br flag
@variable, there is a good article on DO https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.