当前位置:网站首页>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数据
边栏推荐
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- [groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
- 30 optimization skills about mysql, super practical
- Nacos 的安装与服务的注册
- The countdown to the launch of metaverse ape is hot
- Binary tree (III) -- heap sort optimization, top k problem
- Unity Max and min constraint adjustment
- Qtquick3d real time reflection
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- Masked Autoencoders Are Scalable Vision Learners (MAE)
猜你喜欢
第一讲:蛇形矩阵
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Ieventsystemhandler event interface
Exponential weighted average and its deviation elimination
Business introduction of Zhengda international futures company
如何快速体验OneOS
点到直线的距离直线的交点及夹角
Kubernetes Administrator certification (CKA) exam notes (IV)
Qtquick3d real time reflection
随机推荐
实战:fabric 用户证书吊销操作流程
Platformio create libopencm3 + FreeRTOS project
Unity Max and min constraint adjustment
Shelved in TortoiseSVN- Shelve in TortoiseSVN?
opencv 判断点在多边形内外
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
傅里叶分析概述
How to quickly experience oneos
谷歌地图案例
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Tiktok__ ac_ signature
audiopolicy
50. Pow(x, n). O(logN) Sol
VIM tail head intercept file import
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Codeforces Global Round 19
The introduction to go language is very simple: String
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Damn, window in ie open()
509. Fibonacci Number. Sol