9

Nginx Openresty - 读取请求体

 1 year ago
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.
neoserver,ios ssh client

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常用指令 »

wx_ok.pngali_ok.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK