当前位置:网站首页>openresty ngx_ Lua request response
openresty ngx_ Lua request response
2022-07-05 22:53:00 【o_ Guatian Lixia_ o】
openresty ngx_lua Request and response
Request header operation
ngx.req.set_header: add to 、 Modify request header
Grammar format :ngx.req.set_header(name, value)
* name If it doesn't exist , Express addition
* name If there is , To modify
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
ngx.req.set_header(name, "gtlx"): Set individual values
ngx.req.set_header(test, {"1","2"}): Use an array to set multiple values
ngx.req.clear_header: Delete request header
Grammar format :ngx.req.clear_header(name)
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
ngx.req.clear_header(name): Directly delete the request header
ngx.req.set_header(test, nil): By setting to nil, Delete request header
ngx.req.get_headers: Get request header
Grammar format :ngx.req.get_headers(max_headers?, raw?)
Usage environment :set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# Example
location / {
content_by_lua_block {
local ngx = require "ngx";
local headers = ngx.req.get_headers();
for key,value in pairs(headers) do
print(key .. value)
end
ngx.say(h["name"]) # Get the name name Request header for
# Equate to :ngx.var.http_name
}
}
Ask for gymnastics
lua_need_request_body: Force get request body , It is not read by default
Grammar format :lua_need_request_body on|off
* off( Default ): Indicates that the request body data is not read , adopt ngx.var.request_body The data read is empty
* on: Enable reading request body data ,ngx_lua It is not recommended to read the request body in this way
matters needing attention
* If the read request body data exceeds client_body_buffer_size size ,
$request_body、ngx.var.request_body The data read is empty
* client_body_buffer_size、client_max_body_size The settings are the same , Avoid this situation
ngx.req.read_body: Open read request body
grammar :ngx.req.read_body()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*
* Read the request body synchronously , And will not block nginx The event loop
ngx.req.get_body_file: Read the request body data from the temporary file
grammar :file_name = ngx.req.get_body_file()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*
* Get the temporary file storing the requested data , And read the data , Return... As a string
* Try to control the size of the request body , Avoid using temporary files
ngx.req.get_body_data: Read the request body from memory , Return string
grammar :data = ngx.req.get_body_data()
Environmental Science :rewrite_by_lua*、access_by_lua*、content_by_lua*、log_by_lua*
* Read the request body from memory , Return string
ngx.req.get_post_args: Read request data from memory , With table Format return
grammar :args, err = ngx.req.get_post_args(max_args?)
* Read the request body from memory , return table data
* max_args Indicates the maximum number of parameters read
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
Response header operation
ngx.header.headerName: add to 、 modify 、 Delete response header
grammar :ngx.header.headerName = value、ngx.header["headerName"] = value
* headerName There is , Indicates modifying the response header
* headerName non-existent , Indicates adding a response header
* value Set to nil, Indicates deleting the response header
* ngx.header.test_value = value: When the output , Will automatically switch to test-value = value
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
ngx.resp.get_headers: Get response header
grammar :headers = ngx.resp.get_headers(max_headers?, raw?)
* Read response header , With table Type return
Environmental Science :rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、balancer_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
Response body operation
ngx.print、ngx.say: Asynchronous output response body
ngx.flush: Forced to refresh print、say The content of , Convert to synchronous output
Grammar format :
* ngx.print(...): Asynchronous output , Wait for the contents to reach the buffer and output
* ngx.say(...): Asynchronous output , Wait for the contents to reach the buffer and output
* ngx.flush(): Forced to refresh ,flush Output the previous content immediately
Examples of use
default.conf
server {
listen 80;
server_name localhost;
location /test {
content_by_lua_block {
ngx.say("hello");
ngx.sleep(3);
ngx.say("gtlx");
}
}
location /test2 {
content_by_lua_block {
ngx.say("hello");
ngx.flush();
ngx.sleep(3);
ngx.say("gtlx 2");
}
}
location /test3 {
content_by_lua_block {
ngx.say("hello");
ngx.flush(true);
ngx.sleep(3);
ngx.say("gtlx 3");
}
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
Create a container
docker run -it -d -p 3000:80 \
-v /Users/huli/lua/openresty/conf7/default.conf:/etc/nginx/conf.d/default.conf \
--name open4 lihu12344/openresty
Use tests
# Wait for everything 3s Post output
[email protected] conf7 % curl localhost:3000/test
hello
gtlx
# hello First, the output ,gtlx 2 wait for 3s Post output
[email protected] conf7 % curl localhost:3000/test2
hello
gtlx 2
# hello First, the output ,gtlx 2 wait for 3s Post output
[email protected] conf7 % curl localhost:3000/test3
hello
gtlx 3
Examples of use 2
default.conf
server {
listen 80;
server_name localhost;
location /test {
client_body_buffer_size 1k;
client_max_body_size 1k;
content_by_lua_block {
ngx.req.read_body()
ngx.say(ngx.req.get_body_data())
}
}
location /test2 {
client_body_buffer_size 1k;
client_max_body_size 1k;
content_by_lua_block {
ngx.req.read_body();
local args, err = ngx.req.get_post_args();
if args then
for key,value in pairs(args) do
if type(value) == "table" then
ngx.say(key, " ==> ", table.concat(value, " "))
else
ngx.say(key, " ==> ", value)
end
end
else
local file = ngx.req.get_body_file()
if file then
ngx.say("file body", file)
end
end
}
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
Create a container
docker run -it -d -p 4000:80 \
-v /Users/huli/lua/openresty/conf7/default.conf:/etc/nginx/conf.d/default.conf \
--name open5 lihu12344/openresty
***********
Use tests
# /test
[email protected] conf7 % curl -i localhost:4000/test -d "test=123&test=234&name= liable to lay oneself open to suspicion "
HTTP/1.1 200 OK
Server: openresty/1.21.4.1
Date: Tue, 05 Jul 2022 12:56:50 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
test=123&test=234&name= liable to lay oneself open to suspicion
# /test2
[email protected] conf7 % curl -i localhost:4000/test2 -d "test=123&test=234&name= liable to lay oneself open to suspicion "
HTTP/1.1 200 OK
Server: openresty/1.21.4.1
Date: Tue, 05 Jul 2022 12:56:32 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
name ==> liable to lay oneself open to suspicion
test ==> 123 234
get request
post form data
post json data
边栏推荐
- Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Qtquick3d real time reflection
- SPSS analysis of employment problems of college graduates
- Event trigger requirements of the function called by the event trigger
- GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
- 30 optimization skills about mysql, super practical
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- [untitled]
- 分布式解决方案之TCC
- Binary tree (II) -- code implementation of heap
猜你喜欢
Unity Max and min constraint adjustment
谷歌地图案例
MCU case -int0 and INT1 interrupt count
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
The difference between MVVM and MVC
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Overview of Fourier analysis
The countdown to the launch of metaverse ape is hot
透彻理解JVM类加载子系统
随机推荐
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
[untitled]
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
航海日答题小程序之航海知识竞赛初赛
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
Finally understand what dynamic planning is
Arduino 测量交流电流
openresty ngx_lua请求响应
Binary tree (II) -- code implementation of heap
Unity Max and min constraint adjustment
Nangou Gili hard Kai font TTF Download with installation tutorial
I closed the open source project alinesno cloud service
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
抖音__ac_signature
70. Climbing Stairs. Sol
Nanjing: full use of electronic contracts for commercial housing sales
Function default parameters, function placeholder parameters, function overloading and precautions
第一讲:蛇形矩阵
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]