大网数据广告活动
服务器运维问答

Nginx, why do I need to specify a server name?

· 52CE 我爱测

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 …

问题原文

原帖链接 · Nicola Pedretti · CC BY-SA 3.0

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!

已采纳解答

原帖链接 · Paul · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-06-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。