Nginx如何处理请求

译自Nginx官方文档:https://nginx.org/en/docs/http/request_processing.html

一、基于命名的server

nginx首先会决定要处理哪一个server的请求。先来看一个配置的例子,下面的配置中有三个虚拟的服务器且监听端口都是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;
    ...
}

这个配置下nginx只能通过请求头部的HOST决定要把请求传到哪一个server。如果HOST的值没有匹配到server或是请求不包含HOST信息,那nginx会将请求传到设置了default_sever的server。在这个配置中虽然都没有显示的设置default_server,但是nginx会默认将第一个server设置为default_server,所以请求会传到第一个server中。当然了,用户也可以自己设置default_server,如下所示:

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

default_server支持0.8.21及以上版本。早期版本需要使用default参数代替。注意default_server是监听端口的属性,而非server_name的属性。

如果需要拒绝所有不包含HOST的请求,那如何避免未命名的server处理请求?只需要定义一个丢掉请求的server即可。

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

这里server_name设置为空字符串,此时就可以匹配到header中没有Host的请求,同时还会返回一个非标准码404并且关闭这个连接。

自0.8.48版本起,这就是默认的配置,所以server_name ""可以忽略不写。早期版本中,主机的hostname就是默认的server_name。

二、基于命名和IP的混合虚拟server

首先看一个更复杂点的配置,几个server分别监听不同的地址:

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;
    ...
}

这个配置文件中,nginx首先根据server块的监听指令检查请求的IP地址和端口。然后根据可以匹配IP地址和端口的server块入口的server_name测试请求头部的Host。如果server_name没有找到,请求将默认交给default_server处理。例如,www.example.com的请求将由192.168.1.1:80处理,因为第一个server在80端口上没有定义www.example.com。

如前所述,默认的server是监听端口的属性,且不同的默认server可能定义在不同的端口上。

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;
    ...
}

三、一个简单的PHP站点配置

现在来看看nginx如何选择location来处理一个简单而又十分经典的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. 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 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首先搜索文字字符串给定的特定的前缀位置。在上面的配置中,唯一的前缀位置是“ /”,并且由于它匹配任何请求,因此要在最后应用。然后,nginx按照配置文件中列出的顺序检查由正则表达式指定的位置。匹配到后停止搜索,nginx将使用此位置。如果没有正则表达式与请求匹配,则nginx使用较早发现的最特定的前缀位置。

请注意,所有类型的位置仅测试没有参数的请求行的URI部分。这样做是因为查询字符串中的参数可以通过几种方式给出,例如:

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

此外,任何人都可以在查询字符串中请求任何内容:
/index.php?page=1&something+else&user=john

现在,让我们看一下在以上配置中如何处理请求:

A request “/logo.gif” is matched by the prefix location “/” first and then by the regular expression “.(gif|jpg|png)KaTeX parse error: Can't use function '\.' in math mode at position 288: …ar expression “\̲.̲(php)”. Therefore, it is handled by the latter location and the request is passed to a FastCGI server listening on localhost:9000. The fastcgi_param directive sets the FastCGI parameter SCRIPT_FILENAME to “/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”.
A request “/about.html” is matched by the prefix location “/” only, therefore, it is handled in this location. 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.
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”, and nginx searches the locations again as if the request had been sent by a client. As we saw before, the redirected request will eventually be handled by the FastCGI server.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值