nginx是如何匹配过来的请求,然后做处理的呢?这个匹配的过程可以分为两步:
1.选择server
2.选择location 
 
选择server
仅仅匹配server name
加入Nginx的配置文件有这么几项:
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仅仅匹配server_name指令。根据Request请求头的"Host"参数,去匹配server_name的指令参数,从而决定路由到哪个虚拟服务器。如果HOST 请求头参数找不到对应的server,或者压根没有这个请求头,那么nginx将会选择默认的——往往是第一个,这里是 server_name example.org www.example.org;   但是你也可以直接指定:
server {
    listen      80 default_server;
    server_name example.net www.example.net;
    ...
}
在listen指令的后面,使用default_server参数就指定了默认的server。注意,这里指令参数default_server是放在指令listen后面,不是server_name后面,是指定一个具体的端口,不是指定一个虚拟机名称,后面会说明原因。
如果请求头Header没有HOST参数,在0.8.48之后的版本,此请求会被丢弃。在之前的版本中,你可以用下面的指令来完成丢弃的功能:
server {
    listen      80;
    server_name "";
    return      444;
}
指令server_name 的参数设为空数组将匹配不带Host请求头的请求。
 
同时匹配server name和IP
来看看同时带有IP和server name的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;
    ...
}
在这种情况下,Ngxin首先根据请求的IP和Port匹配listen指令,然后再根据请求的Host头匹配server_name 指令。比如本机的ip是192.168.1.1,那么所有过来的请求都不会到192.168.1.2上去。如果Host没有匹配上,再用默认的server来处理。
例如: 发往192.168.1.1:80端口的www.example.com请求,只会从前面两个server中去找对应的server_name,发现没有匹配上,那么采用默认的(第一个)server。
我看来看看在多个IP的配置文件下,如何指定默认的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;
    ...
}
所以,server是针对某个IP,某个端口的,并不是针对某个域名,因此default_server也是针对某个IP,某个端口的,是listen的属性而不是server_name 的属性。
 
选择 location 
location指令都是针对请求的uri部分进行匹配,不是整个url,也不包含任何请求参数,比如:
/index.php
/user/index/get
 
Nginx的locating指令可以分为两种,即"prefix location"和"regular expression location",即“前缀location”和“正则location”。
前缀location形如下面:
location / {
       ....
}
location /path/to {
        .....
 }
location /index.php {
       .....
 }
 

正则location location后面需要匹配正则表达式,比如:

  location ~* \.(gif|jpg|png)$ {
        expires 30d;
   }
 
以一个PHP网站的配置为例,看看Nginx如果选择location来处理请求的。
server {
    listen      80;
    server_name example.org www.example.org;
    root        /data/www;
 
    location / {
        index  index.html index.php;
    }
 
    location /comment {
        rewrite /comment/(.*) /$1 break;
    }
 
    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首先在前缀location中进行匹配,而且首先搜寻匹配的最为"精确"的那一项:比如请求/comment,即匹配location /comment 和location / 但是location /comment 更为“精确”因此被匹配上。location / 因为是匹配所有的请求所以被“最后考虑”。这一步匹配是无关前缀location的罗列顺序的。
接着,nginx继续匹配正则location,这一步跟正则location的顺序就有关系了。nginx是依次匹配,第一个匹配的到location时。nginx会终止匹配,然后用这个location来处理请求。如果正则location部分没有匹配到任何一项,则采用上一步前缀location匹配到的来处理请求。
在匹配到location,处理Request时,还需要加上root指令,即root   /data/www;
 
举例说明下:
"/logo.gif"——首先被前缀location的location /匹配上了,然后也被正则location的location ~* \.(gif|jpg|png)$ 匹配上了,因此会被后面的location处理。此外,在处理请求是还要加上root   /data/www ,最为请求就被定为到了 /data/www/logo.gif,最后次文件会被发送到浏览器
 
"/index.php"——同样首先被前缀location的location /匹配上,但后面正则location也匹配上了location ~ \.php$,因此由后者来处理:交给运行在localhost:9000的FASTCGI程序来处理,
fastcgi_param 指令用来设定FASTCGI的参数SCRIPT_FILENAME,$document_root和$fastcgi_script_name是nginx参数,分别为root指令参数(/data/www)和URI(/index.php)
 
"/about.html"——仅仅能被location /匹配,因此便由它处理,最后请求定位到/data/www/about.html
 
"/"——这种情况稍微复杂点。它仅能被前缀location"location /"匹配上,因此它将被这个location处理。然后location中的指令index将被执行,nginx会依次尝试index文件(俗称入口文件)是否存在,在此例中就是/data/www/index.html /data/www/index.php,如果前者不存在就继续往后找。此例中,/data/www/index.html不存在因此由/data/www/index.php处理。然后nginx会做个内部跳转到/index.php,然后,就处理好像一个从浏览器反过来的全新请求一样,重新搜寻location,就像前面分析的一样,最终交由FASTCGI程序来处理。
 
 
 

Nginx是如何处理Request的?的更多相关文章

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

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

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

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

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

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

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

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

  5. nginx 是如何处理过期事件的?

    目录 什么是过期事件 nginx 是如何处理过期事件的? 参考资料 什么是过期事件 对于不需要加入到 post 队列 延后处理的事件,nginx 的事件都是通过 ngx_epoll_process_e ...

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

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

  7. nginx是如何处理一个请求的(包含https配置)

    配置https首先要有ssl证书,这个证书目前阿里有免费的,但如果自己做实验,也是可以自签证书,只不过不受信 openssl genrsa -des3 -out server.key 1024     ...

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

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

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

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

随机推荐

  1. thinkphp5URL和路由

    前面的话 本文将详细介绍thinkphp5URL和路由 URL访问 ThinkPHP采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从URL参数中解析当前请求的模块.控制器和操 ...

  2. CentOS下nginx php mysql 环境搭建

    CentOS下搭建PHP运行环境. 首先是在虚拟机上装好一个命令行的CentOS,如果只是弄服务器的话,不要装图形界面,会比较卡. 一.安装编译工具及库文件 yum -y install make z ...

  3. HTTPS加密流程超详解(一)前期准备

    0.前言 前一阵子想写一个HTTPS的嗅探工具,之前只是大致了解SSL/TLS协议的加密流程,真正上起手来一步一步分析发现还是有点复杂的,于是我参考了wireshark的源码以及各种RFC,弄清楚了S ...

  4. Java并发编程有多难?这几个核心技术你掌握了吗?

    本文主要内容索引 1.Java线程 2.线程模型 3.Java线程池 4.Future(各种Future) 5.Fork/Join框架 6.volatile 7.CAS(原子操作) 8.AQS(并发同 ...

  5. 初次使用Python脚本,proto协议文件转Lua代码

    使用IntelliJ IDEA编辑器编写Lua脚本的时候,安装一个插件 "EmmyLua" 可以对有代码提示功能 想把protoBuf协议文件转成Lua代码,然后给出代码提示 第一 ...

  6. JDK 1.8 源码阅读和理解

    根据 一篇文章教会你,如何做到招聘要求中的“要有扎实的Java基础” 的指引,决定开始阅读下JDK源码. 本文将作为源码阅读总纲 一.精读部分 java.io java.lang java.util ...

  7. ioutil包二

    ioutil包二 (原创随笔,转载请注明出处 http://www.cnblogs.com/majianguo/p/8016426.html) ioutil包实现了一些I/O实用功能,导出了7个函数和 ...

  8. Codebase Refactoring (with help from Go)

    Codebase Refactoring (with help from Go) 代码库重构(借助于Go) 1.摘要 Go应该添加为类型创建替代等效名称的能力,以便在代码库重构期间渐进代码修复.本文解 ...

  9. LeetCode题目总结(一)

    我的代码在github上,https://github.com/WINTERFELLS/LeetCode-Answers 这里只提供个人的解题思路,不一定是最好的. Problems1-20 寻找两个 ...

  10. 深入了解CSS字体度量,行高和vertical-align

    line-height和vertical-align在CSS中是两个简单的属性.如此简单,大多数人都相信自己已经完全理解它们是如何工作的以及如何使用它们.但事实上并不如此.他们其实很复杂,也是CSS中 ...