当前位置:网站首页>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"
}
边栏推荐
- MySQL backup and recovery + experiment
- Application and Optimization Practice of redis in vivo push platform
- Win:使用 PowerShell 检查无线信号的强弱
- Practical case of SQL optimization: speed up your database
- pytorch fine-tuning (funtune) : 镂空设计or 偷梁换柱
- Huawei machine test question: longest continuous subsequence
- February database ranking: how long can Oracle remain the first?
- Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
- runc hang 导致 Kubernetes 节点 NotReady
- [source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
猜你喜欢
Yyds dry inventory swagger positioning problem ⽅ formula
PowerShell: use PowerShell behind the proxy server
Naacl 2021 | contrastive learning sweeping text clustering task
官宣!第三届云原生编程挑战赛正式启动!
MATLB | multi micro grid and distributed energy trading
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Exploration and Practice of Stream Batch Integration in JD
Video display and hiding of imitation tudou.com
随机推荐
Practical case of SQL optimization: speed up your database
Include rake tasks in Gems - including rake tasks in gems
Restful fast request 2022.2.1 release, support curl import
Valentine's Day flirting with girls to force a small way, one can learn
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
Open source SPL optimized report application coping endlessly
Common bit operation skills of C speech
Missile interception -- UPC winter vacation training match
How to build a technical team that will bring down the company?
He was laid off.. 39 year old Ali P9, saved 150million
如何做一个炫酷的墨水屏电子钟?
Summary of regularization methods
Using druid to connect to MySQL database reports the wrong type
runc hang 导致 Kubernetes 节点 NotReady
Practice of tdengine in TCL air conditioning energy management platform
[understanding of opportunity -38]: Guiguzi - Chapter 5 flying clamp - warning one: there is a kind of killing called "killing"
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Win: add general users to the local admins group
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by