当前位置:网站首页>openresty ngx_lua执行阶段
openresty ngx_lua执行阶段
2022-07-05 02:17:00 【o_瓜田李下_o】
openresty ngx_lua执行阶段
nginx http执行阶段
nginx http执行阶段
# ngx_http_core_module.h
typedef enum {
NGX_HTTP_POST_READ_PHASE = 0,
NGX_HTTP_SERVER_REWRITE_PHASE,
NGX_HTTP_FIND_CONFIG_PHASE,
NGX_HTTP_REWRITE_PHASE,
NGX_HTTP_POST_REWRITE_PHASE,
NGX_HTTP_PREACCESS_PHASE,
NGX_HTTP_ACCESS_PHASE,
NGX_HTTP_POST_ACCESS_PHASE,
NGX_HTTP_PRECONTENT_PHASE,
NGX_HTTP_CONTENT_PHASE,
NGX_HTTP_LOG_PHASE
} ngx_http_phases;
执行阶段说明
NGX_HTTP_POST_READ_PHASE = 0,
* 接受并读取请求
NGX_HTTP_SERVER_REWRITE_PHASE,
* 修改url阶段,通常有重定向、变量设置
NGX_HTTP_FIND_CONFIG_PHASE,
* 查找url对应的配置,如匹配location
NGX_HTTP_REWRITE_PHASE,
* 匹配到location后,可能会再次进入NGX_HTTP_SERVER_REWRITE_PHASE
NGX_HTTP_POST_REWRITE_PHASE,
* 检查url是否执行过阶段4,如果执行了,再次进入阶段3
* 最大检查次数为10,超过会报错
NGX_HTTP_PREACCESS_PHASE,
* 请求资源限速等操作
NGX_HTTP_ACCESS_PHASE,
* 访问控制,如限制ip访问
NGX_HTTP_POST_ACCESS_PHASE,
* 验证NGX_HTTP_ACCESS_PHASE(访问控制)结果,如果通过则继续
NGX_HTTP_PRECONTENT_PHASE,
* try_files指令时生效
NGX_HTTP_CONTENT_PHASE,
* 处理http请求内容,一般需要与后端应用进行交互
NGX_HTTP_LOG_PHASE
* 日志输出
ngx_lua 执行阶段
执行顺序
init_by_lua_block
init_worker_by_lua_block
set_by_lua_block
rewrite_by_lua_block
access_by_lua_block
content_by_lua_block
header_filter_by_lua_block
body_filter_by_lua_block
log_filter_by_lua_block
init_by_lua_block:nginx初始化、重启时执行,在http模块中设置
http {
include mime.types;
default_type application/octet-stream;
init_by_lua_block {
local uuid = require 'resty.jit-uuid';
uuid.seed();
}
...
}
init_by_lua_file:作用与init_by_lua_block相同,代码块保存在文件中
http {
include mime.types;
default_type application/octet-stream;
init_worker_by_lua_file /usr/local/nginx/conf/init.lua
...
}
# /usr/local/nginx/conf/init.lua 文件内容
local uuid = require 'resty.jit-uuid';
uuid.seed();
init_worker_by_lua_block:master进程启动后,执行相关代码块,在http模块中设置
# 可用来执行定时任务、后端健康检查
http {
include mime.types;
default_type application/octet-stream;
init_worker_by_lua_block {
...
}
...
}
set_by_lua_block:执行代码块,并将结果返回,在server、location语句块中设置
语法格式:set_by_lua_block $result {...}
* 该命令是阻塞命令,执行的语句块尽可能短、快,避免耗时过多
* 语句块中禁用:ngx.say()、ngx.exit()、ngx.sleep()等命令
server {
listen 80;
server_name localhost;
set $a "";
set_by_lua_block $a {
return "1";
}
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
...
}
rewrite_by_lua_block:重写阶段执行,可在http、server、location中设置
语法格式:rewrite_by_lua_block {...}
* 语句块会在最后执行,可通过rewrite_by_lua_no_postpone(默认off)改变执行顺序
* 可调用所有ngx api
# a的值为2
server {
listen 80;
server_name localhost;
set $a "1";
rewrite_by_lua_block {
ngx_var_a="2";
}
set $a "3"
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
...
}
# http阶段配置:rewrite_by_lua_no_postpone on
# a的值为3
server {
listen 80;
server_name localhost;
set $a "1";
rewrite_by_lua_block {
ngx_var_a="2";
}
set $a "3"
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
...
}
access_by_lua_block:access阶段执行语句块(权限检查、黑白名单),在http、server、location中设置
语法格式:access_by_lua_block {...}
* 语句块会在最后执行,可通过access_by_lua_no_postpone(默认off)改变执行顺序
* 可调用所有ngx api
* 应用:将黑白名单存放在redis、共享内存中,动态设置黑白名单
content_by_lua_block:内容处理阶段执行,设置输出内容,在location中设置
语法格式:content_by_lua_block {...}
* 可调用所有ngx api
* 若与其他内容执行阶段语句同时使用,content_by_lua_block可能不会执行
location / {
content_by_lua_block {
ngx.say("hello gtlx");
}
echo "test" #只会输出test,不会输出hello gtlx
}
content_by_lua_file:与content_by_lua_block相同,语句保存在文件中,在location中设置
语法格式:content_by_lua_file file
* 可调用所有ngx api
location / {
content_by_lua_file /usr/local/openresty/file/test.lua
}
# 文件:/usr/local/openresty/file/test.lua
hello gtlx
balancer_by_lua_block:内容处理阶段执行,在upstream语句中使用
语法格式:balancer_by_lua_block {...}
* upstream使用的ip为balancer_by_lua_block中设置的地址
* 禁用的api:cosockets、light threads
upstream web-server {
server 172.18.0.2 #会忽略server设置的ip地址
balancer_by_lua_block {
... #真是使用的ip地址
}
}
header_filter_by_lua_block:响应阶段添加、删除响应头,在http、server、location中设置
语法格式:header_filter_by_lua_block {...}
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等
# 添加响应头
location {
header_filter_by_lua_block {
ngx.header.test="hello gtlx"
}
echo "hello"
}
body_filter_by_lua_block:响应阶段修改响应体,在http、server、location中设置
语法格式:body_filter_by_lua_block {...}
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等
# 将相应数据转换为大写(ngx.arg[1]获取响应数据,ngx.arg[2]响应结束符)
# 输出:HELLO
location {
body_filter_by_lua_block {
ngx.arg[1] = string.upper(ngx.arg[1]);
}
echo "hello"
}
log_by_lua_block:输出日志,可在http、server、location中设置
语法格式:log_filter_by_lua_block {...}
* 在access_log之前执行,不会替换当前的access_log日志
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等
location {
log_filter_by_lua_block {
ngx.log("hello gtlx")
}
echo "hello"
}
边栏推荐
- Application and Optimization Practice of redis in vivo push platform
- Variables in postman
- Codeforces Global Round 19 ABC
- MATLB|多微电网及分布式能源交易
- Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
- Huawei machine test question: longest continuous subsequence
- RichView TRVUnits 图像显示单位
- The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
- Es uses collapsebuilder to de duplicate and return only a certain field
猜你喜欢
PowerShell: use PowerShell behind the proxy server
如何搭建一支搞垮公司的技术团队?
MySQL regexp: Regular Expression Query
Interesting practice of robot programming 14 robot 3D simulation (gazebo+turtlebot3)
A tab Sina navigation bar
Action News
Yolov5 model training and detection
官宣!第三届云原生编程挑战赛正式启动!
Security level
One plus six brushes into Kali nethunter
随机推荐
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by
MATLB|多微电网及分布式能源交易
runc hang 导致 Kubernetes 节点 NotReady
[uc/os-iii] chapter 1.2.3.4 understanding RTOS
Interpretation of mask RCNN paper
Phpstrom setting function annotation description
Abacus mental arithmetic test
[illumination du destin - 38]: Ghost Valley - chapitre 5 Flying clamp - one of the Warnings: There is a kind of killing called "hold Kill"
Yolov5 model training and detection
Naacl 2021 | contrastive learning sweeping text clustering task
Include rake tasks in Gems - including rake tasks in gems
Visual studio 2019 set transparent background (fool teaching)
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Application and Optimization Practice of redis in vivo push platform
Learn tla+ (XII) -- functions through examples
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
Binary tree traversal - middle order traversal (golang)
力扣剑指offer——二叉树篇
Yyds dry inventory swagger positioning problem ⽅ formula
Comment mettre en place une équipe technique pour détruire l'entreprise?