

Nginx Openresty - 读取请求体
source link: http://abcdxyzk.github.io/blog/2022/10/30/nginx-lua-body/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx Openresty - 读取请求体
2022-10-30 15:33:00
https://blog.csdn.net/trustnature/article/details/94417336
由于nginx默认不读取请求体的数据,因此当 Lua 通过 ngx.var.request_body 的方式获取请求体时会发现数据为空,那么,该如何获得请求体的数据呢?
方式一:lua_need_request_body
语法:lua_need_request_body<on|off>
默认值:off
环境:http、server、location、
含义:默认为off,即不读取请求体。如果设置为on,则表示强制读取请求体,此时,可以通过 ngx.var.request_body 来获取请求体的数据。但需要注意一种情况,$request_body 存在与内存中,如果它的字节大小超过Nginx配置的 client_body_buffer_size 的值,Nginx就会把请求体存放到临时文件中。此时数据就不存在与内存中了,这会导致 $request_body 为空,所以需要设置 client_body_buffer_size 和 client_max_body_size 的值相同,避免出现这种情况。
这种配置方式不够灵活,Ngx_Lua官网也不推荐使用此方法。
方式二:ngx.req.read_body
语法:ngx.req.read_body()
环境:rewrite_by_lua*、access_by_lua*、content_by_lua*
含义:同步读取客户端请求体,且不会阻塞Nginx的事件循环。使用此指令后,就可以通过 ngx.req.get_body_data 来获取请求体的数据了。按如果使用临时文件来存放请求体,就需要先使用函数 ngx.req.get_body_file 来获取临时文件名,再读取临时文件中的请求体数据。
指令: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.read_body 指令后,可以使用本指令在内存中获取请求体数据,结果会返回一个Lua的字符串类型数据。如果要获取Lua的table类型的数据,则需要使用 ngx.req.get_post_args
指令:ngx.req.get_post_data
语法:args,err=ngx.req.get_post_args(max_args?)
环境:rewrite_by_lua*、access_by_lua*、content_by_lua*、header_filter_by_lua*、body_filter_by_lua*、log_by_lua*
含义:在执行 ngx.req.read_body 指令后,可以使用本指令读取包含当前请求在内的所有POST值请求的查询参数,返回一个Lua的table类型的数据。max_args 参数的作用时限制参数的数量。为了服务的安全,最多支持使用100个参数(包括重复的参数),超过限制的参数会被忽略。如果max_args为0,则表示关闭此限制;但为了避免被无穷多的参数攻击,不要将max_args设置为0,如果最多支持使用10个参数,则应配置为 ngx.req.get_post_args(10)。
使用场景:
content_by_lua_block {
local ngx = require "ngx"
ngx.req.read_body()
local data = ngx.req.get_body_data()
if data then
ngx.print('ngx.req.get_body_data:',data,')
return
else
lcoal file = ngx.req.get_body_file()
if file then
ngx.say("body is in file",file)
else
ngx.say("no body found")
end
end
}
Posted by kk
2022-10-30 15:33:00tools, nginx
« Nginx 安装 Lua 支持 Lua-Nginx-Module常用指令 »


Recommend
-
102
OpenResty - Turning Nginx into a Full-Fledged Scriptable Web Platform Table of Contents Description OpenResty is a full-fledged web application server by bundling the standard nginx core, lots of 3rd-party nginx modules,...
-
48
从 http.Request.Body 或 http.Response.Body 中读取数据方法或许很多,标准库中大多数使用 ioutil.ReadAll 方法一次读取所有数据,如果是 json 格式的数据还可以使用 json.N...
-
55
-
40
之前发布 【Go】优雅的读取http请求或响应的数据续 文章,网友 “wxe” 咨询:“优化前后的请求耗时变化有多大”,之前...
-
61
-
3
在 AspNetCore 中读取当前请求的请求体 (Request.Body) 最近有需要在 AspNet Core 应用中读取当前请求的请求体 (Request.Body) , 本来以为是很简单的事情, 没想到居然有坑, 因此记录如下。 动作方法 (Action Method) 没有参数的...
-
6
【Go】优雅的读取http请求或响应的数据 2019-01-26 ...
-
3
【Go】优雅的读取http请求或响应的数据-续 2019-02-01 ...
-
9
Nginx/OpenResty 指令的执行顺序 May 21, 2015 http, 可以通过 init_by_lua 加载公共函数,比如 lua-resty-core. server selection,listen...
-
6
通过Nginx(OpenResty)修改UserAgent - 干雨 - 博客园 其实我是一个程序员 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK