官网:How nginx processes a request
翻译部分:Name-based virtual servers(基于名称的虚拟服务器)
开始!
nginx first decides which server should process the request.
nginx首先决定哪个server块应该处理请求。
Let’s start with a simple configuration where all three virtual servers listen on port *:80:
让我们以一个简单的配置文件开始,配置文件的所有3个虚拟服务器都监听80端口。
server { listen 80; server_name example.org www.example.org; ... } server { listen 80; server_name example.net www.example.net; ... } server { listen 80; server_name example.com www.example.com; ... }
In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to.
在这个配置文件中,nginx只测试请求头的host字段来决定路由请求到哪个虚拟服务器,host如下图:
If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.
如果请求的host都没有匹配到server_name,或者请求根本不包含这个头字段,然后,nginx会路由这个请求到这个端口的默认服务器(即server块)。
In the configuration above, the default server is the first one — which is nginx’s standard default behaviour.
上面的配置文件的默认服务器是第一个-这是nginx的标准默认行为。
It can also be set explicitly which server should be default, with the
default_server
parameter in the listen directive:
还可以用在listen指令上default_server参数来明确设置默认服务器,如下:
server { listen 80 default_server; server_name example.net www.example.net; ... }
Thedefault_server
parameter has been available since version 0.8.21. In earlier versions thedefault
parameter should be used instead.
default_server参数自0.8.21版本使用,在这之前的版本用default参数。
Note that the default server is a property of the listen port and not of the server name.
注意,default_server是监听端口listen指令的一个属性,而不是server_name指令的属性。
More about this later.
接下来会有更多内容。