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

Name-based virtual servers 给予名称的虚拟服务的更多相关文章

  1. Storages and virtual servers

    1. IBM Storages: SONAS,V7k,V7ku,SVC,XIV 存储设备都安装了个性化定制的Linux系统,来完成不同的服务,这几台存储设备使用原理都是类似的,以SONAS (Scal ...

  2. 基于名称的虚拟主机-Apache

    基于名称的虚拟主机和基于IP的虚拟主机的对比 基于IP的虚拟主机使用连接的IP地址来识别(区分)正确的虚拟主机,所以对于每一个虚拟主机,你都需要有独立的IP地址. 基于名称的虚拟主机,服务器依赖于客户 ...

  3. Understanding and Managing SMTP Virtual Servers

    Simple Mail Transfer Protocol (SMTP) Service Overview The Simple Mail Transfer Protocol (SMTP) servi ...

  4. Virtual servers on a Raspberry Pi with the light weight OS virtualization system Docker!

    转自:http://www.hyggeit.dk/2014/02/virtual-servers-on-raspberry-pi-with.html Virtual servers on a Rasp ...

  5. SSL Virtual Servers

    SSL Virtual Servers 来源 https://www.carlstalhood.com/ssl-virtual-servers-netscaler-12/ SSL Virtual Se ...

  6. Chapter 1 Securing Your Server and Network(4):使用虚拟服务帐号

    原文:Chapter 1 Securing Your Server and Network(4):使用虚拟服务帐号 原文出处:http://blog.csdn.net/dba_huangzj/arti ...

  7. LVS---linux虚拟服务(一)

    一.集群cluster 1.集群的产生 2.集群的类型 3.分布式系统 4.集群和分布式的区别 二.LVS 1.为什么使用LVS? 2.LVS术语 3.LVS架构 4.LVS集群的类型 5.关于会话保 ...

  8. Istio VirtualService 虚拟服务

    概念及示例 VirtualService 描述了一个或多个用户可寻址目标到网格内实际工作负载之间的映射 . 虚拟服务让您配置如何在服务网格内将请求路由到服务,这基于 Istio 和平台提供的基本的连通 ...

  9. 【Linux】虚拟服务器之LVS

    写在前面 觉得甚是幸运,能够有机会参与到ITOO配置环境的工作中去.现在正在熟悉,在搭建环境的时候,有LVS安装配置教程,对这一块有些懵逼,这几天查了一些资料,写在这里,和大家分享一下 是什么 LVS ...

随机推荐

  1. 机器学习 —— 概率图模型(Homework: CRF Learning)

    概率图模型的作业越往后变得越来越有趣了.当然,难度也是指数级别的上涨啊,以至于我用了两个周末才完成秋名山神秘车牌的寻找,啊不,CRF模型的训练. 条件随机场是一种强大的PGM,其可以对各种特征进行建模 ...

  2. C中调用LUA回调(LUA注册表)

    实现原理: 通过将LUA中得回调函数存入LUA注册表中来保存LUA函数,然后在需要回调时从LUA注册表中取出LUA函数进行调用 下面是一些预备知识:(学习两个重要的函数) 原汁原味的英文解释的最透彻, ...

  3. PHP的面向对象编程

    面向对象编程的概念: 不同的作者之间说法可能不一样,但是一个OOP语言必须有以下几方面: 抽象数据类型和信息封装 继承 多态 在PHP中是通过类来完成封装的: <?php class Somet ...

  4. Android HTTPS(2)HttpURLConnection.getInputStream异常的原因及解决方案

    Common Problems Verifying Server Certificates InputStream in = urlConnection.getInputStream(); getIn ...

  5. 投影纹理映射(Projective Texture Mapping)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人”  投影纹理映射( Projective ...

  6. 函数lock_rec_add_to_queue

    在原来的type_mode基础上,加上LOCK_REC /*********************************************************************// ...

  7. Linux查看CPU信息

    1.  查看物理CPU的个数 #cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc –l 2.   查看逻辑CPU的个数 #ca ...

  8. /etc/selinux/config

    /etc/selinux/configSELINUX=disabled改成了SELINUX=enforcing机器无法启动 linux无法启动怎么解决:[1]selinux配置错误 SELinux 入 ...

  9. (转) Python Generators(生成器)——yield关键字

    http://blog.csdn.net/scelong/article/details/6969276 生成器是这样一个函数,它记住上一次返回时在函数体中的位置.对生成器函数的第二次(或第 n 次) ...

  10. XE7 - 升级及初步使用

    春节没抢到回家的票,正好有时间把Delphi2010升级到了XE7. 用了快一个月了,今天算是补记. 安装包用了lsuper大侠整理的lsuper.XE7.Update1.v10.1.拜谢!比较顺利的 ...