我们先定义一个location块级指令phase_echo来处理客户端发过来请求URI处理。
location /phase_echo { set $name "Tinywan"; echo $name; set $name "开源技术小栈"; echo $name; set $name "Tinywan 开源技术小栈"; echo $name;}
请求访问输出结果:
PS C:/Users/Tinywan/Desktop> curl -i http://openresty.tinywan.com/phase_echoHTTP/1.1 200 OKServer: openresty/1.17.8.2Date: Sun, 14 Jul 2024 00:31:18 GMTContent-Type: application/octet-streamTransfer-Encoding: chunkedConnection: keep-aliveTinywan 开源技术小栈Tinywan 开源技术小栈Tinywan 开源技术小栈
为什么输出全部是Tinywan 开源技术小栈。也就是最后一个设置的变量值呢?前面设置的怎么都没生效吗?
这是因为Nginx处理每一个用户请求时,都是按照若干个不同阶段依次处理的,而不是根据配置文件上的顺序。以上配置涉及到了 两个阶段 rewrite和content阶段。
而实际执行执行是 rewrite阶段的指令在 content阶段指令之前执行。实际的执行顺序应当是以下这样子的。
set $name "Tinywan";set $name "开源技术小栈";set $name "Tinywan 开源技术小栈";echo $name;echo $name;echo $name;
所以这就是为什么最终会输出Tinywan 开源技术小栈。带着以上配置文件中执行的指令,让我们进入Nginx执行流程与阶段详解。
Nginx处理请求的过程一共划分为11个阶段,按照执行顺序依次是post-read、server-rewrite、find-config、rewrite、post-rewrite、 preaccess、access、post-access、try-files、content、log。
所以整个请求的过程,是按照不同的阶段执行的,在某个阶段执行完该阶段的指令之后,再进行下一个阶段的指令执行。
执行阶段示例
图片
图片
图片来源:https://yxudong.github.io。
OpenResty发起一个请求时,会有相应的执行流程,Nginx与Lua编写脚本的基本构建块是指令执行顺序的。
从图中可知OpenResty 处理请求大致分为4个大阶段,11个小阶段。
这些指令通常有三种形式
这边推荐使用 xxx_by_lua_file,它彻底分离了配置文件与业务代码,让两者可以独立部署,而且文件形式也让我们更容易以模块的方式管理组织 Lua 程序。
图片
图片来源:https://blog.51cto.com/lisea/2425794。
server { listen 80; server_name openresty.tinywan.com; location /run_phase { set_by_lua_block $a { ngx.log(ngx.ERR, "Tinywan is set_by_lua_block phase") } rewrite_by_lua_block { ngx.log(ngx.ERR, "Tinywan is rewrite_by_lua_block phase") } access_by_lua_block { ngx.log(ngx.ERR, "Tinywan is access_by_lua_block phase") } content_by_lua_block { ngx.log(ngx.ERR, "Tinywan is content_by_lua_block phase") } header_filter_by_lua_block { ngx.log(ngx.ERR, "Tinywan is header_filter_by_lua_block phase") } body_filter_by_lua_block { ngx.log(ngx.ERR, "Tinywan is body_filter_by_lua_block phase") } log_by_lua_block { ngx.log(ngx.ERR, "Tinywan is log_by_lua_block phase") } }}
执行请求访问:
curl -i http://openresty.tinywan.com/run_phase
查看错误日志文件内容:
2024/07/13 12:38:43 [error] 7#7: *2 [lua] set_by_lua:2: Tinywan is set_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] rewrite_by_lua(openresty.tinywan.com.conf:18):2: Tinywan is rewrite_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] access_by_lua(openresty.tinywan.com.conf:22):2: Tinywan is access_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] content_by_lua(openresty.tinywan.com.conf:26):2: Tinywan is content_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] header_filter_by_lua:2: Tinywan is header_filter_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] body_filter_by_lua:2: Tinywan is body_filter_by_lua_block phase, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"2024/07/13 12:38:43 [error] 7#7: *2 [lua] log_by_lua(openresty.tinywan.com.conf:38):2: Tinywan is log_by_lua_block phase while logging request, client: 172.18.0.1, server: openresty.tinywan.com, request: "GET /run_phase HTTP/1.1", host: "openresty.tinywan.com"
通过日志文件记录可以看到执行是按照阶段顺序进行输出。
本文链接:http://www.28at.com/showinfo-26-100984-0.htmlOpenResty实战系列 | 执行流程与阶段详解
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com