当前位置:网站首页>openresty ngx_lua请求响应
openresty ngx_lua请求响应
2022-07-05 22:43:00 【o_瓜田李下_o】
openresty ngx_lua请求响应
请求头操作
ngx.req.set_header:添加、修改请求头
语法格式:ngx.req.set_header(name, value)
* name 如果不存在,表示添加
* name 如果存在,表示修改
使用环境:set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# 示例
ngx.req.set_header(name, "gtlx"):设置单个值
ngx.req.set_header(test, {"1","2"}):使用数组设置多个值
ngx.req.clear_header:删除请求头
语法格式:ngx.req.clear_header(name)
使用环境:set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# 示例
ngx.req.clear_header(name):直接删除请求头
ngx.req.set_header(test, nil):通过设置为nil,删除请求头
ngx.req.get_headers:获取请求头
语法格式:ngx.req.get_headers(max_headers?, raw?)
使用环境:set_by_lua*、rewrite_by_lua*
access_by_lua*、content_by_lua*
header_filter_by_lua*、body_filter_by_lua*
# 示例
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"]) #获取名为name的请求头
#等同于:ngx.var.http_name
}
}
请求体操作
lua_need_request_body:强制获取请求体,默认不读取
语法格式:lua_need_request_body on|off
* off(默认):表示不读取请求体数据,通过ngx.var.request_body读取的数据为空
* on:开启读取请求体数据,ngx_lua不推荐使用这种方式读取请求体
注意事项
* 如果读取的请求体数据超过client_body_buffer_size大小,
$request_body、ngx.var.request_body读取的数据为空
* client_body_buffer_size、client_max_body_size设置相同,避免出现这种情况
ngx.req.read_body:开启读取请求体
语法:ngx.req.read_body()
环境:rewrite_by_lua*、access_by_lua*、content_by_lua*
* 同步读取请求体,且不会阻塞nginx事件循环
ngx.req.get_body_file:从临时文件读取请求体数据
语法:file_name = ngx.req.get_body_file()
环境:rewrite_by_lua*、access_by_lua*、content_by_lua*
* 获取存放请求数据的临时文件,并读取数据,以字符串返回
* 尽量控制请求体的大小,避免使用临时文件
ngx.req.get_body_data:从内存中读取请求体,返回字符串
语法:data = ngx.req.get_body_data()
环境:rewrite_by_lua*、access_by_lua*、content_by_lua*、log_by_lua*
* 从内存中读取请求体,返回字符串
ngx.req.get_post_args:从内存中读取请求数据,以table格式返回
语法:args, err = ngx.req.get_post_args(max_args?)
* 从内存中读取请求体,返回table数据
* max_args表示最多读取的参数个数
环境:rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
响应头操作
ngx.header.headerName:添加、修改、删除响应头
语法:ngx.header.headerName = value、ngx.header["headerName"] = value
* headerName存在,表示修改响应头
* headerName不存在,表示添加响应头
* value设置为nil,表示删除响应头
* ngx.header.test_value = value:输出时,会自动转换为test-value = value
环境:rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
ngx.resp.get_headers:获取响应头
语法:headers = ngx.resp.get_headers(max_headers?, raw?)
* 读取响应头,以table类型返回
环境:rewrite_by_lua*、access_by_lua*、
content_by_lua*、log_by_lua*、balancer_by_lua*、
header_filter_by_lua*、body_filter_by_lua*
响应体操作
ngx.print、ngx.say:异步输出响应体
ngx.flush:强制刷新print、say的内容,转换为同步输出
语法格式:
* ngx.print(...):异步输出,等待内容全部到达缓冲区后输出
* ngx.say(...):异步输出,等待内容全部到达缓冲区后输出
* ngx.flush():强制刷新,flush将之前的内容立刻输出
使用示例
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;
}
}
创建容器
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
使用测试
# 全部内容等待3s后输出
[email protected] conf7 % curl localhost:3000/test
hello
gtlx
# hello先输出,gtlx 2等待3s后输出
[email protected] conf7 % curl localhost:3000/test2
hello
gtlx 2
# hello先输出,gtlx 2等待3s后输出
[email protected] conf7 % curl localhost:3000/test3
hello
gtlx 3
使用示例 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;
}
}
创建容器
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
***********
使用测试
# /test
[email protected] conf7 % curl -i localhost:4000/test -d "test=123&test=234&name=瓜田李下"
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=瓜田李下
# /test2
[email protected] conf7 % curl -i localhost:4000/test2 -d "test=123&test=234&name=瓜田李下"
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 ==> 瓜田李下
test ==> 123 234
get 请求
post form数据
post json数据
边栏推荐
- a-tree 树的全部展开和收起
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- 第一讲:蛇形矩阵
- 实战:fabric 用户证书吊销操作流程
- 如何快速体验OneOS
- Thinkphp5.1 cross domain problem solving
- Event trigger requirements of the function called by the event trigger
- Distributed resource management and task scheduling framework yarn
- MCU case -int0 and INT1 interrupt count
- Vcomp110.dll download -vcomp110 What if DLL is lost
猜你喜欢
一文搞定class的微观结构和指令
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Qtquick3d real time reflection
Overview of Fourier analysis
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
谷歌地图案例
Distributed resource management and task scheduling framework yarn
航海日答题小程序之航海知识竞赛初赛
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Opencv judgment points are inside and outside the polygon
随机推荐
Thinkphp5.1 cross domain problem solving
How to create a thread
我把开源项目alinesno-cloud-service关闭了
Metaverse Ape上线倒计时,推荐活动火爆进行
Google Maps case
Shelved in TortoiseSVN- Shelve in TortoiseSVN?
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
【Note17】PECI(Platform Environment Control Interface)
Nail error code Encyclopedia
分布式解决方案之TCC
The difference between MVVM and MVC
链表之双指针(快慢指针,先后指针,首尾指针)
BFC block level formatting context
SPSS analysis of employment problems of college graduates
Error when LabVIEW opens Ni instance finder
Postman核心功能解析-参数化和测试报告
Roman numeral to integer
Metaverse Ape获Negentropy Capital种子轮融资350万美元
[agc009e] eternal average - conclusion, DP
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)