Nginx处理请求的11个阶段(agentzh的Nginx 教程学习记录) 客官°小女子只卖身不卖艺 2021-06-24 16:09 436阅读 0赞 Nginx 处理请求的过程一共划分为 11 个阶段,按照执行顺序依次是 post-read、server-rewrite、find-config、rewrite、post-rewrite、preaccess、access、post-access、try-files、content 以及 log。 ## 1、post-read ## 最先执行的 post-read 阶段在 Nginx 读取并解析完请求头(request headers)之后就立即开始运行。例如:使用了 [ngx\_realip][ngx_realip] 模块提供的 [set\_real\_ip\_from][set_real_ip_from] 和 [real\_ip\_header][real_ip_header] 这两条配置指令 ## 2、server-rewrite ## 由于 server-rewrite 阶段位于 post-read 阶段之后,所以 server 配置块中的 [set][] 指令也就总是运行在 [ngx\_realip][ngx_realip] 模块改写请求的来源地址之后。 ## 3、find-config ## 这个阶段并不支持 Nginx 模块注册处理程序,而是由 Nginx 核心来完成当前请求与 location 配置块之间的配对工作。换句话说,在此阶段之前,请求并没有与任何 location 配置块相关联。因此,对于运行在 find-config 阶段之前的 post-read 和 server-rewrite 阶段来说,只有 server 配置块以及更外层作用域中的配置指令才会起作用。这就是为什么只有写在 server 配置块中的 [ngx\_rewrite][ngx_rewrite] 模块的指令才会运行在 server-rewrite 阶段,这也是为什么前面所有例子中的 [ngx\_realip][ngx_realip] 模块的指令也都特意写在了 server 配置块中,以确保其注册在 post-read 阶段的处理程序能够生效。 ## 4、rewrite ## 由于 Nginx 已经在 find-config 阶段完成了当前请求与 location 的配对,所以从 rewrite 阶段开始,location 配置块中的指令便可以产生作用。当 [ngx\_rewrite][ngx_rewrite] 模块的指令用于 location 块中时,便是运行在这个 rewrite 阶段。 ## 5、post-rewrite ## 这个阶段也像 find-config 阶段那样不接受 Nginx 模块注册处理程序,而是由 Nginx 核心完成 rewrite 阶段所要求的“内部跳转”操作(如果 rewrite 阶段有此要求的话)。例如:通过 [rewrite][] 指令把当前请求的 URI 无条件地改写为 /bar,同时发起一个“内部跳转”,最终跳进了 location /bar 中。这里比较有趣的地方是“内部跳转”的工作原理。“内部跳转”本质上其实就是把当前的请求处理阶段强行倒退到 find-config 阶段,以便重新进行请求 URI 与 location 配置块的配对。 ## 6、preaccess ## 该阶段在 access 阶段之前执行,故名 preaccess.标准模块 [ngx\_limit\_req][ngx_limit_req] 和 [ngx\_limit\_zone][ngx_limit_zone] 就运行在此阶段,前者可以控制请求的访问频度,而后者可以限制访问的并发度。 ## 7、access ## 标准模块 [ngx\_access][ngx_access]、第三方模块 [ngx\_auth\_request][ngx_auth_request] 以及第三方模块 [ngx\_lua][ngx_lua] 的 [access\_by\_lua][access_by_lua] 指令就运行在这个阶段。 ## 8、 post-access ## 这个阶段也和 post-rewrite 阶段类似,并不支持 Nginx 模块注册处理程序,而是由 Nginx 核心自己完成一些处理工作。post-access 阶段主要用于配合 access 阶段实现标准 [ngx\_http\_core][ngx_http_core] 模块提供的配置指令 [satisfy][] 的功能。对于多个 Nginx 模块注册在 access 阶段的处理程序, [satisfy][] 配置指令可以用于控制它们彼此之间的协作方式。比如模块 A 和 B 都在 access 阶段注册了与访问控制相关的处理程序,那就有两种协作方式,一是模块 A 和模块 B 都得通过验证才算通过,二是模块 A 和模块 B 只要其中任一个通过验证就算通过。第一种协作方式称为 all 方式(或者说“与关系”),第二种方式则被称为 any 方式(或者说“或关系”)。默认情况下,Nginx 使用的是 all 方式。 ## 9、try-files ## 这个阶段专门用于实现标准配置指令 [try\_files][try_files] 的功能,并不支持 Nginx 模块注册处理程序。[try\_files][try_files] 指令接受两个以上任意数量的参数,每个参数都指定了一个 URI. 这里假设配置了 N 个参数,则 Nginx 会在 try-files 阶段,依次把前 N-1 个参数映射为文件系统上的对象(文件或者目录),然后检查这些对象是否存在。一旦 Nginx 发现某个文件系统对象存在,就会在 try-files 阶段把当前请求的 URI 改写为该对象所对应的参数 URI(但不会包含末尾的斜杠字符,也不会发生 “内部跳转”)。如果前 N-1 个参数所对应的文件系统对象都不存在,try-files 阶段就会立即发起“内部跳转”到最后一个参数(即第 N 个参数)所指定的 URI.通过 [root][] 配置指令所指定的“文档根目录”进行映射。例如,当“文档根目录”是 /var/www/ 的时候,请求 URI /foo/bar 会被映射为文件 /var/www/foo/bar,而请求 URI /foo/baz/ 则会被映射为目录 /var/www/foo/baz/. 注意这里是如何通过 URI 末尾的斜杠字符是否存在来区分“目录”和“文件”的。 ## 10、content ## Nginx 的 content 阶段是所有请求处理阶段中最为重要的一个,因为运行在这个阶段的配置指令一般都肩负着生成“内容”(content)并输出 HTTP 响应的使命。正因为其重要性,这个阶段的配置指令也异常丰富。echo、 [Nginx 变量漫谈(二)][Nginx] 中接触到的 [echo\_exec][echo_exec] 指令, [Nginx 变量漫谈(三)][Nginx 1] 中接触到的 [proxy\_pass][proxy_pass] 指令,[Nginx 变量漫谈(五)][Nginx 2] 中介绍过的 [echo\_location][echo_location] 指令,以及 [Nginx 变量漫谈(七)][Nginx 3] 中介绍过的 [content\_by\_lua][content_by_lua]。 ## 11、log ## log阶段处理,比如记录访问量/统计平均响应时间。log\_by\_lua 参考资料:[https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html][https_openresty.org_download_agentzh-nginx-tutorials-zhcn.html] [ngx_realip]: http://wiki.nginx.org/HttpRealIpModule [set_real_ip_from]: http://wiki.nginx.org/HttpRealIpModule#set_real_ip_from [real_ip_header]: http://wiki.nginx.org/HttpRealIpModule#real_ip_header [set]: http://wiki.nginx.org/HttpRewriteModule#set [ngx_rewrite]: http://wiki.nginx.org/HttpRewriteModule [rewrite]: http://wiki.nginx.org/HttpRewriteModule#rewrite [ngx_limit_req]: http://wiki.nginx.org/HttpLimitReqModule [ngx_limit_zone]: http://wiki.nginx.org/HttpLimitZoneModule [ngx_access]: http://wiki.nginx.org/HttpAccessModule [ngx_auth_request]: http://mdounin.ru/hg/ngx_http_auth_request_module/ [ngx_lua]: http://wiki.nginx.org/HttpLuaModule [access_by_lua]: http://wiki.nginx.org/HttpLuaModule#access_by_lua [ngx_http_core]: http://nginx.org/en/docs/http/ngx_http_core_module.html [satisfy]: http://wiki.nginx.org/HttpCoreModule#satisfy [try_files]: http://wiki.nginx.org/HttpCoreModule#try_files [root]: http://wiki.nginx.org/HttpCoreModule#root [Nginx]: https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#01-NginxVariables02 [echo_exec]: http://wiki.nginx.org/HttpEchoModule#echo_exec [Nginx 1]: https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#01-NginxVariables03 [proxy_pass]: http://wiki.nginx.org/HttpProxyModule#proxy_pass [Nginx 2]: https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#01-NginxVariables05 [echo_location]: http://wiki.nginx.org/HttpEchoModule#echo_location [Nginx 3]: https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html#01-NginxVariables07 [content_by_lua]: http://wiki.nginx.org/HttpLuaModule#content_by_lua [https_openresty.org_download_agentzh-nginx-tutorials-zhcn.html]: https://openresty.org/download/agentzh-nginx-tutorials-zhcn.html
还没有评论,来说两句吧...