Name-based virtual servers 给予名称的虚拟服务

nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port *:80:

nginx 首先决定哪个服务应该处理请求。让我们开始一个简单的配置,这个配置含有含有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”的请求头部,以确定到要路由到哪个服务器

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. In the configuration above,

 然后nginx将路由请求到默认这个端口服务器。在上边的配置中

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:

他也能够被制定设置,这个服务器应该是默认的,带有“默认服务”参数在监听的指令:

server {
    listen      80 default_server; 监听  80默认服务
    server_name example.net www.example.net; 服务器  例子.net,www.例子.net
    ...
}

 

The default_server parameter has been available since version 0.8.21. In earlier versions the default parameter should be used instead.
这个默认服务器参数已经是有效的从0.8.21版本开始。在早期这个默认参数应该被使用替代

Note that the default server is a property of the listen port and not of the server name. More about this later.

注意默认服务器上一个监听端口属性,不是服务器名称,更多信息请继续关注

How to prevent processing requests with undefined server names

如何防止处理请求与未定义的服务器名称

If requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined:

如果不允许没有“主机”头字段的请求,只需将请求的服务器定义为可以被定义的服务器:

server {
    listen      80;
    server_name "";
    return      444;
}

Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx’s non-standard code 444 is returned that closes the connection.

这里,服务器名称被设置成空,他将匹配没有"host"头部字段的请求,并且一个特别的nginx's没有标准代码444作为关闭连接被返回

Since version 0.8.48, this is the default setting for the server name, so the server_name "" can be omitted. In earlier versions, the machine’s hostname was used as a default server name.
0.8.48版以来,这是服务器名称的默认设置,所以server_name”可以省略。在早期版本中,机器的主机名作为默认的服务器名称。

 

Mixed name-based and IP-based virtual servers

基于混合名称的基于IP的虚拟服务器

Let’s look at a more complex configuration where some virtual servers listen on different addresses:

 让我们看看一个更复杂的配置,其中一些虚拟服务器在不同的地址上侦听:
server {
    listen      192.168.1.1:80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      192.168.1.1:80;
    server_name example.net www.example.net;
    ...
}

server {
    listen      192.168.1.2:80;
    server_name example.com www.example.com;
    ...
}

In this configuration, nginx first tests the IP address and port of the request against the listen directives of the server blocks.

在这种配置中,Nginx的第一次测试的IP地址和服务器模块对听指令请求端口。

It then tests the “Host” header field of the request against the server_name entries of the server blocks that matched the IP address and port. If the server name is not found,

然后测试对服务器的块匹配的IP地址和端口的server_name条目请求“主机”头字段。如果找不到服务器名称,

the request will be processed by the default server.

 请求将被默认服务器处理。
For example,

a request for www.example.com received on the 192.168.1.1:80 port will be handled by the default server of the 192.168.1.1:80 port, i.e., by the first server, since there is no www.example.com defined for this port.

i.e. 例如

一种在192.168.1.1:80端口接收的请求将在www. 192.168.1.1:80端口,即默认的服务器处理,由第一个服务器,由于没有www.这个端口定义。

 

As already stated, a default server is a property of the listen port, and different default servers may be defined for different ports:

正如已经声明的那样,默认服务器是监听端口的属性,不同的默认服务器可以定义为不同的端口

server {
    listen      192.168.1.1:80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      192.168.1.1:80 default_server;
    server_name example.net www.example.net;
    ...
}

server {
    listen      192.168.1.2:80 default_server;
    server_name example.com www.example.com;
    ...
}

 

A simple PHP site configuration

Now let’s look at how nginx chooses a location to process a request for a typical, simple PHP site:

现在让我们来看看nginx怎么样选择一个位置来处理典型的、简单的Php站点

server {
    listen      80;
    server_name example.org www.example.org;
    root        /data/www;

    location / {
        index   index.html index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires 30d;
    }

    location ~ \.php$ {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME
                      $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }
}

 

nginx first searches for the most specific prefix location given by literal strings regardless of the listed order.

nginx首先寻找最匹配的后缀位置常量字符床,并且不管字符串常量在列表中的位置

In the configuration above the only prefix location is “/

在前边的配置中这个紧有的前缀位置是“/”

and since it matches any request it will be used as a last resort.

因为它符合任何要求,它将被用来作为最后的手段

Then nginx checks locations given by regular expression

然后nginx检车被设定正则表达式的位置

in the order listed in the configuration file.

在配置文件的排序列表中

The first matching expression stops the search and nginx will use this location.

寻找到第一个匹配的表达式就停止,并且将这个将用这个位置的配置

If no regular expression matches a request, then nginx uses the most specific prefix location found earlier.

如果没有正则匹配到请求,nginx用之前找到的最特别制定的后缀Location "\"?

Note that locations of all types test only a URI part of request line without arguments. This is done because arguments in the query string may be given in several ways, for example:

注意所有类型测试的位置紧紧是没有参数请求的URL部分。这样做的原因是参数在不通的参数字符串中有不同的方式给出,例如:

/index.php?user=john&page=1
/index.php?page=1&user=john

Besides, anyone may request anything in the query string:

除外,任何人请求下边的查找字符串的内容可能不同

/index.php?page=1&something+else&user=john

 

Now let’s look at how requests would be processed in the configuration above:

现在让我们看看在上边的配置文件中请求将如何被处理

  • A request “/logo.gif” is matched by the prefix location “/” first and then by the regular expression “\.(gif|jpg|png)$”,
  • 一个请求“/logo.gif”首先被特定的位置"、"匹配,然后被正则表达式"\.(gif|jpg|png)$"所匹配
  • therefore, it is handled by the latter location.
  • 所以,他被后来的哪个位置操中
  • Using the directive “root /data/www” the request is mapped to the file /data/www/logo.gif, and the file is sent to the client.
  • 用指令"root  /data/www"这个请求被映射到/data/www/logo.fif,然后文件被发送到客户端
  • A request “/index.php” is also matched by the prefix location “/” first and then by the regular expression “\.(php)$”.
  • 指令"/index.php"也被制定的"/"首先匹配,然后被正则表达式"\.(php)$"匹配
  • Therefore, it is handled by the latter location and the request is passed to a FastCGI server listening on localhost:9000.
  • 所以,后来的位置被选中,然后请求被用到监听9000端口的本地服务器:
  • The fastcgi_param directive sets the FastCGI parameter SCRIPT_FILENAME to “/data/www/index.php”,
  • 这个快速的通用网关接口参数指令设置参数文件到"/data/www/index.php"
  • and the FastCGI server executes the file.
  • 并且这个快速通用网关接口服务执行这个文件
  • The variable $document_root is equal to the value of the root directive and the variable $fastcgi_script_name is equal to the request URI, i.e. “/index.php”.
  • 这个变量 $document_root和根指令的值是相同的。并且变量$fastcgi_script_name 是和请求URL是相等的,比如"/index.php"
  • A request “/about.html” is matched by the prefix location “/” only, therefore, it is handled in this location.
  • 一个请求"/about.html"紧紧被"/"匹配到,所以操纵这个位置
  • Using the directive “root /data/www” the request is mapped to the file /data/www/about.html, and the file is sent to the client.
  • 使用指令"root /data/www"这个请求被匹配到/data/www/about.html,然后文件被发送到客户端
  • Handling a request “/” is more complex. It is matched by the prefix location “/” only,
  • 操作一个请求"/"是更加复杂。他紧紧被位置"/"匹配
  • therefore, it is handled by this location.
  • 所以,他被这个位置操纵
  • Then the index directive tests for the existence of index files according to its parameters and the “root /data/www” directive.
  •  然后对索引文件存在的指标指导试验根据其参数和“根/数据/网络”的指令
  • If the file /data/www/index.html does not exist, and the file /data/www/index.php exists, then the directive does an internal redirect to “/index.php”,
  •  如果文件/数据/网络/文件不存在,并且文件/数据/网络/的index.php的存在,然后指令并内部重定向到“/index.php
  • and nginx searches the locations again as if the request had been sent by a client. nginx搜索位置再次如请求被客户端发送
  • As we saw before, the redirected request will eventually be handled by the FastCGI server.
  • 正如我们之前看到的,重定向的请求最终将由FastCGI服务器处理。

written by Igor Sysoev
edited by Brian Mercer

posted @ 2016-09-13 16:52  刘小草  阅读(345)  评论(0编辑  收藏  举报