4

I am curious as of why there is a need to specify a server_name in the nginx configuration file on top of the listen property.

I have been reading a digitalocean article on understanding nginx configuration file, and this is how it explains the difference between the two settings:

listen: The ip address / port combination that this server block is designed to respond to. If a request is made by a client that matches these values, this block will potentially be selected to handle the connection.

server_name: This directive is the other component used to select a server block for processing. If there are multiple server blocks with listen directives of the same specificity that can handle the request, Nginx will parse the "Host" header of the request and match it against this directive.

But how could there be two different servers listening on the same ip and the same port? Linux would not even allow you to start two services listening on the same port as far as I know.

What am I missing?

Thanks!

5
  • 1
    One server, many virtual hosts. Like if you point example.com and example.org at the same IP. Commented Jun 29, 2017 at 19:08
  • but could they both be listening on the same port? Commented Jun 29, 2017 at 19:11
  • One nginx process can handle more than one domain name. Yes, you can host an infinite number of domains on the same port using virtualhosts. en.wikipedia.org/wiki/Virtual_hosting Commented Jun 29, 2017 at 19:12
  • Oh, maybe I understand. So if one IP was actually pointing to multiple servers then multiple applications could be listening on the same port because they are actually on different machines. Then nginx would have to decide based on the name which server you actually meant. Is this correct ? Commented Jun 29, 2017 at 19:21
  • 1
    No, it has nothing to do with multiple servers and multiple applications. Nginx listens on port 80 and 443. A request comes in for example.com. Nginx looks for a server {} block with a server_name of example.com. If a request comes in for example.org, it looks for a server block with example.org in the server_name. Commented Jun 29, 2017 at 19:23

2 Answers 2

3

Nginx is one service on Linux, and it is using the ports you tell it to use. From there, Linux is not concerned with what nginx does, so if nginx wants to read something in a request to the reserved port (e.g., get example.com on port 80) and do something different with it from other requests (e.g., get example.net on port 80), it can do so.

Since one server commonly hosts more than one domain, nginx and all modern web server software offer a feature to support this, which in nginx is the server_name directive.

However, if nginx wanted to do something on the same port as some other service on the server (e.g., port 22, commonly used by OpenSSH), then there would be a conflict to resolve.

1
  • Thanks Paul. I already had the answer in the comments but I think you did a good job at explaining it. Hopefully this will help other people Commented Jul 2, 2017 at 21:41
0

Well, with HTTP 1.1 you could point to one IP many domain names, it was not possible in HTTP 1.0(more you can see in this answer). So, you start on nginx process and it listening on requested ports, infinite number of domains and subdomains. For example you could check stackoverflow.com and serverfault.com, they both points to same IPs.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.