原文:http://nginx.org/en/docs/http/request_processing.html

server_name directive 参考:http://nginx.org/en/docs/http/server_names.html

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:

  1. server {
  2. listen 80;
  3. server_name example.org www.example.org;
  4. ...
  5. }
  6.  
  7. server {
  8. listen 80;
  9. server_name example.net www.example.net;
  10. ...
  11. }
  12.  
  13. server {
  14. listen 80;
  15. server_name example.com www.example.com;
  16. ...
  17. }

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. 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, the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server parameter in the listen directive:

  1. server {
  2. listen 80 default_server;
  3. server_name example.net www.example.net;
  4. ...
  5. }

The default_server parameter has been available since version 0.8.21. In earlier versions the default parameter should be used instead.

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:

  1. server {
  2. listen 80;
  3. server_name "";
  4. return 444;
  5. }

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.

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.

Mixed name-based and IP-based virtual servers

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

  1. server {
  2. listen 192.168.1.1:80;
  3. server_name example.org www.example.org;
  4. ...
  5. }
  6.  
  7. server {
  8. listen 192.168.1.1:80;
  9. server_name example.net www.example.net;
  10. ...
  11. }
  12.  
  13. server {
  14. listen 192.168.1.2:80;
  15. server_name example.com www.example.com;
  16. ...
  17. }

In this configuration, nginx first tests the IP address and port of the request against the listen directives of the server blocks. 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, 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.

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

  1. server {
  2. listen 192.168.1.1:80;
  3. server_name example.org www.example.org;
  4. ...
  5. }
  6.  
  7. server {
  8. listen 192.168.1.1:80 default_server;
  9. server_name example.net www.example.net;
  10. ...
  11. }
  12.  
  13. server {
  14. listen 192.168.1.2:80 default_server;
  15. server_name example.com www.example.com;
  16. ...
  17. }

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:

  1. server {
  2. listen 80;
  3. server_name example.org www.example.org;
  4. root /data/www;
  5.  
  6. location / {
  7. index index.html index.php;
  8. }
  9.  
  10. location ~* \.(gif|jpg|png)$ {
  11. expires 30d;
  12. }
  13.  
  14. location ~ \.php$ {
  15. fastcgi_pass localhost:9000;
  16. fastcgi_param SCRIPT_FILENAME
  17. $document_root$fastcgi_script_name;
  18. include fastcgi_params;
  19. }
  20. }

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.

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:

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

Besides, anyone may request anything in the query string:

  1. /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)$”, 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.
  • A request “/index.php” is also matched by the prefix location “/” first and then by the regular 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.
written by Igor Sysoev
edited by Brian Mercer

2-How nginx processes a request的更多相关文章

  1. 多进程端口监听 How nginx processes a request Server names

    网络编程( 六):端口那些事儿 - 知乎专栏  https://zhuanlan.zhihu.com/p/20365900 不停服务reload.restart 多进程端口监听 我们都有一个计算机网络 ...

  2. nginx 出现413 Request Entity Too Large问题的解决方法

    nginx 出现413 Request Entity Too Large问题的解决方法 使用php上传图片(大小1.9M),出现 nginx: 413 Request Entity Too Large ...

  3. Nginx出现“413 Request Entity Too Large”错误解决方法

    Nginx出现“413 Request Entity Too Large”错误解决方法 2011-03-25 13:49:55|  分类: 默认分类 |  标签:413  request  entit ...

  4. Nginx 出现413 Request Entity Too Large得解决方法

    Nginx 出现413 Request Entity Too Large得解决方法 默认情况下使用nginx反向代理上传超过2MB的文件,会报错413 Request Entity Too Large ...

  5. wordpress 主题安装 您点击的链接已过期 nginx 出现413 Request Entity Too Large

    1 nginx 出现413 Request Entity Too Large 问题是限制上传大小,解决: 1.打开nginx配置文件 nginx.conf, 路径一般是:/etc/nginx/ngin ...

  6. nginx 自动忽略request中header name包含下划线参数的解决方法

    使用nginx过程中遇到了个问题,就是request中的header name中如果包含下划线会自动忽略掉,导致服务器接收不到该字段的内容,以下为解决方法: nginx默认request的header ...

  7. Nginx出现413 Request Entity Too Large错误解决方法

    Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx.conf,找到http{}段,添加 解决方法就是 打 ...

  8. Nginx是如何处理Request的?

    nginx是如何匹配过来的请求,然后做处理的呢?这个匹配的过程可以分为两步: 1.选择server 2.选择location    选择server 仅仅匹配server name 加入Nginx的配 ...

  9. Nginx:413 Request Entity Too Large解决

    最近在做给博客添加上传PDF的功能,但是在测试上传文件的过程中遇到了413 Request Entity Too Large错误.不过这个无错误是很好解决的,这个错误的出现是因为上传的文件大小超过了N ...

随机推荐

  1. ubuntu 默认python版本切换

    电脑上面有些脚本是python2的,有些是python3的,但是系统默认是python2,需要设置环境变量来进行切换. python2切换到python3: echo alias python=pyt ...

  2. Vue 从入门到进阶之路(十二)

    之前的文章我们介绍了一下 vue 中插槽的使用,本章我们接着介绍一下 vue 中的作用域插槽. <!DOCTYPE html> <html lang="en"&g ...

  3. Python 词云可视化

    最近看到不少公众号都有一些词云图,于是想学习一下使用Python生成可视化的词云,上B站搜索教程的时候,发现了一位UP讲的很不错,UP也给出了GitHub上的源码,是一个很不错的教程,这篇博客主要就是 ...

  4. Windowns系统下搭建python环境

    本文介绍下在windows系统下安装python和python环境搭建. 安装PYTHON 首先,我们去python的官方网站下载python安装包.官网地址:https://www.python.o ...

  5. PHP mysqli_stat MySQLi 函数

    定义和用法 mysqli_stat - 获取当前系统状态信息 版本支持 PHP4 PHP5 PHP7 不支持 支持 支持 语法 mysqli_stat ( mysqli $link ) mysqli_ ...

  6. 【C#】学习笔记 Linq相关

    Language-Integrated Query(语言集成查询) 写了个demo,具体看

  7. 数据处理之以OLEDB方式读取Excel数据丢失的原因及解决方法

    1.引言 在应用程序的设计中,经常需要读取Excel数据或将Excel数据导入转换到其他数据载体中,C#读取Excel的方式有两种,一种是通过OLEDB方式读取,另一种为通过COM组件方式读取.近段时 ...

  8. iOS随记

    ios 10 访问设置问题 从ios8之api支持访问设置通过访问UIApplicationOpenSettingsURLString来跳转设置 NSURL*url=[NSURL URLWithStr ...

  9. 30-学容器必须懂 bridge 网络

    Docker 安装时会创建一个 命名为 docker0 的 linux bridge.如果不指定--network,创建的容器默认都会挂到 docker0 上. apt-get install bri ...

  10. 如何使用压缩的方式将Windows下的zip压缩包上传到Linux系统

      我们可以使用在Windows下压缩文件夹,然后到Linux系统下解压缩的方式,完成整个上传工作. 第一步:在Windows系统下,将整个文件夹压缩成zip后缀的压缩包 方法一: