当前位置:网站首页>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
边栏推荐
- 基于STM32的ADC采样序列频谱分析
- The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
- 3 find the greatest common divisor and the least common multiple
- Common model making instructions
- Request preview display of binary data and Base64 format data
- Arduino 测量交流电流
- 【无标题】
- Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
- d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
- Binary tree (III) -- heap sort optimization, top k problem
猜你喜欢
One article deals with the microstructure and instructions of class
【无标题】
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
Distance entre les points et les lignes
Exponential weighted average and its deviation elimination
2022软件测试工程师涨薪攻略,3年如何达到30K
Hcip day 12 (BGP black hole, anti ring, configuration)
a-tree 树的全部展开和收起
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
利用LNMP实现wordpress站点搭建
随机推荐
509. Fibonacci Number. Sol
一文搞定垃圾回收器
fibonacci search
分布式解决方案选型
记录几个常见问题(202207)
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
C language - structural basis
50. Pow(x, n). O(logN) Sol
The countdown to the launch of metaverse ape is hot
[untitled]
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Usage Summary of scriptable object in unity
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Yiwen gets rid of the garbage collector
Function default parameters, function placeholder parameters, function overloading and precautions
基于STM32的ADC采样序列频谱分析
解决thinkphp启动时“No input file specified”的问题
Navigation day answer applet: preliminary competition of navigation knowledge competition
二叉树(三)——堆排序优化、TOP K问题