当前位置:网站首页>openresty ngx_ Lua execution phase
openresty ngx_ Lua execution phase
2022-07-05 02:18:00 【o_ Guatian Lixia_ o】
openresty ngx_lua Execution phase
nginx http Execution phase
nginx http Execution phase
# 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;
Description of the implementation phase
NGX_HTTP_POST_READ_PHASE = 0,
* Accept and read the request
NGX_HTTP_SERVER_REWRITE_PHASE,
* modify url Stage , Usually there is redirection 、 Variable is set
NGX_HTTP_FIND_CONFIG_PHASE,
* lookup url Corresponding configuration , If it matches location
NGX_HTTP_REWRITE_PHASE,
* Match to location after , May enter again NGX_HTTP_SERVER_REWRITE_PHASE
NGX_HTTP_POST_REWRITE_PHASE,
* Check url Whether the stage has been executed 4, If executed , Enter the stage again 3
* The maximum number of checks is 10, More than will report error
NGX_HTTP_PREACCESS_PHASE,
* Request resource speed limit and other operations
NGX_HTTP_ACCESS_PHASE,
* Access control , Such as restrictions ip visit
NGX_HTTP_POST_ACCESS_PHASE,
* verification NGX_HTTP_ACCESS_PHASE( Access control ) result , If yes, continue
NGX_HTTP_PRECONTENT_PHASE,
* try_files The directive takes effect
NGX_HTTP_CONTENT_PHASE,
* Handle http Request content , Generally, you need to interact with back-end applications
NGX_HTTP_LOG_PHASE
* Log output
ngx_lua Execution phase

Execution order
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 initialization 、 Execute on restart , stay http Set in the module
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: The functions and init_by_lua_block identical , The code block is saved in a file
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 The contents of the document
local uuid = require 'resty.jit-uuid';
uuid.seed();
init_worker_by_lua_block:master After the process starts , Execute related code blocks , stay http Set in the module
# It can be used to perform scheduled tasks 、 Back end health check
http {
include mime.types;
default_type application/octet-stream;
init_worker_by_lua_block {
...
}
...
}
set_by_lua_block: Execute code block , And return the result , stay server、location Set
Grammar format :set_by_lua_block $result {...}
* This command is a blocking command , The executed statement block should be as short as possible 、 fast , Avoid taking too much time
* Disable :ngx.say()、ngx.exit()、ngx.sleep() Wait for the order
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: Rewrite phase execution , Can be found in http、server、location Set in
Grammar format :rewrite_by_lua_block {...}
* The statement block will be executed at the end , It can be done by rewrite_by_lua_no_postpone( Default off) Change the order of execution
* All... Can be called ngx api
# a The value of is 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 Stage configuration :rewrite_by_lua_no_postpone on
# a The value of is 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 Stage execution statement block ( Permission check 、 Black and white list ), stay http、server、location Set in
Grammar format :access_by_lua_block {...}
* The statement block will be executed at the end , It can be done by access_by_lua_no_postpone( Default off) Change the order of execution
* All... Can be called ngx api
* application : Store the black and white list in redis、 In shared memory , Dynamically set the black and white list
content_by_lua_block: The content processing phase executes , Set the output content , stay location Set in
Grammar format :content_by_lua_block {...}
* All... Can be called ngx api
* If used in conjunction with other content execution phase statements ,content_by_lua_block May not execute
location / {
content_by_lua_block {
ngx.say("hello gtlx");
}
echo "test" # Only output test, No output hello gtlx
}
content_by_lua_file: And content_by_lua_block identical , Statements are saved in a file , stay location Set in
Grammar format :content_by_lua_file file
* All... Can be called ngx api
location / {
content_by_lua_file /usr/local/openresty/file/test.lua
}
# file :/usr/local/openresty/file/test.lua
hello gtlx
balancer_by_lua_block: The content processing phase executes , stay upstream Use in statement
Grammar format :balancer_by_lua_block {...}
* upstream The use of ip by balancer_by_lua_block The address set in
* Forbidden api:cosockets、light threads
upstream web-server {
server 172.18.0.2 # Will ignore server Set up ip Address
balancer_by_lua_block {
... # It's really used ip Address
}
}
header_filter_by_lua_block: The response phase adds 、 Delete response header , stay http、server、location Set in
Grammar format :header_filter_by_lua_block {...}
* Forbidden api:ngx.say()、ngx.redirect()、ngx.exec() etc.
# Add response header
location {
header_filter_by_lua_block {
ngx.header.test="hello gtlx"
}
echo "hello"
}
body_filter_by_lua_block: In the response phase, modify the response body , stay http、server、location Set in
Grammar format :body_filter_by_lua_block {...}
* Forbidden api:ngx.say()、ngx.redirect()、ngx.exec() etc.
# Convert the corresponding data to uppercase (ngx.arg[1] Get response data ,ngx.arg[2] Response terminator )
# Output :HELLO
location {
body_filter_by_lua_block {
ngx.arg[1] = string.upper(ngx.arg[1]);
}
echo "hello"
}
log_by_lua_block: Output log , Can be found in http、server、location Set in
Grammar format :log_filter_by_lua_block {...}
* stay access_log Before execution , Will not replace the current access_log journal
* Forbidden api:ngx.say()、ngx.redirect()、ngx.exec() etc.
location {
log_filter_by_lua_block {
ngx.log("hello gtlx")
}
echo "hello"
}
边栏推荐
- STL container
- ICSI 311 Parser
- Advanced learning of MySQL -- Application -- Introduction
- Naacl 2021 | contrastive learning sweeping text clustering task
- Win:使用组策略启用和禁用 USB 驱动器
- Unpool(nn.MaxUnpool2d)
- One plus six brushes into Kali nethunter
- Do you know the eight signs of a team becoming agile?
- RichView TRVStyle MainRVStyle
- When to catch an exception and when to throw an exception- When to catch the Exception vs When to throw the Exceptions?
猜你喜欢

如何做一个炫酷的墨水屏电子钟?

The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns

MATLB|多微电网及分布式能源交易

R语言用logistic逻辑回归和AFRIMA、ARIMA时间序列模型预测世界人口

Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
![[技术发展-26]:新型信息与通信网络的数据安全](/img/13/10c8bd340017c6516edef41cd3bf6f.png)
[技术发展-26]:新型信息与通信网络的数据安全

Android advanced interview question record in 2022

Visual explanation of Newton iteration method

runc hang 导致 Kubernetes 节点 NotReady

How to build a technical team that will bring down the company?
随机推荐
187. Repeated DNA sequence - with unordered_ Map basic content
Stored procedure and stored function in Oracle
Application and Optimization Practice of redis in vivo push platform
Start the remedial work. Print the contents of the array using the pointer
[機緣參悟-38]:鬼穀子-第五飛箝篇 - 警示之一:有一種殺稱為“捧殺”
"C zero foundation introduction hundred knowledge and hundred cases" (72) multi wave entrustment -- Mom shouted for dinner
Binary tree traversal - middle order traversal (golang)
Yolov5 model training and detection
Why do you understand a16z? Those who prefer Web3.0 Privacy Infrastructure: nym
Practice of tdengine in TCL air conditioning energy management platform
Tucson will lose more than $400million in the next year
Lsblk command - check the disk of the system. I don't often use this command, but it's still very easy to use. Onion duck, like, collect, pay attention, wait for your arrival!
Go RPC call
Interpretation of mask RCNN paper
Richview trvunits image display units
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
力扣剑指offer——二叉树篇
One plus six brushes into Kali nethunter
CAM Pytorch
Visual explanation of Newton iteration method