当前位置:网站首页>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数据


边栏推荐
- Navigation day answer applet: preliminary competition of navigation knowledge competition
- QT creator 7 beta release
- 2022软件测试工程师涨薪攻略,3年如何达到30K
- Character conversion PTA
- Thinkphp5.1 cross domain problem solving
- Google Maps case
- 90后测试员:“入职阿里,这一次,我决定不在跳槽了”
- How to create a thread
- Ieventsystemhandler event interface
- Hcip day 16
猜你喜欢

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

Google Maps case

Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题

Lesson 1: serpentine matrix

Nangou Gili hard Kai font TTF Download with installation tutorial

Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space

513. Find the value in the lower left corner of the tree

Spectrum analysis of ADC sampling sequence based on stm32

audiopolicy

SPSS analysis of employment problems of college graduates
随机推荐
TOPSIS code part of good and bad solution distance method
My experience and summary of the new Zhongtai model
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Tiktok__ ac_ signature
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
Go语言学习教程(十五)
How to quickly experience oneos
Nacos 的安装与服务的注册
Masked Autoencoders Are Scalable Vision Learners (MAE)
航海日答题小程序之航海知识竞赛初赛
一文搞定class的微观结构和指令
Shelved in TortoiseSVN- Shelve in TortoiseSVN?
QT creator 7 beta release
Distance entre les points et les lignes
Vcomp110.dll download -vcomp110 What if DLL is lost
分布式解决方案选型
Overriding equals() & hashCode() in sub classes … considering super fields
MCU case -int0 and INT1 interrupt count
Some tutorials install the database on ubantu so as not to occupy computer memory?
50. Pow(x, n). O(logN) Sol