http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size

该地址对于client_body_buffer_size配置做了说明

syntax: client_body_buffer_size size

default: client_body_buffer_size 8k|16k

context: http,server,location

Sets buffer size for reading client request body. In case the request body is larger than the buffer, the whole body or only its part is written to a temporary file. By default, buffer size is equal to two memory pages. This is 8K on x86, other 32-bit platforms, and x86-64. It is usually 16K on other 64-bit platforms.

为读取客户端请求体设置缓存区大小。为了防止请求体超过缓存区大小,将整个请求体或其中一分部写入一个临时文件。默认缓存区大小为two memory pages。在x86、32位机器、x86-64上是8k,其它64位机器上是16k。

解决方案有两种:

1、调大client_body_buffer_size值

client_body_buffer_size 128k

client_max_body_size 100m

这两个参数配合使用,如果总body_size超过这个值,请求会被拒绝

2、去临时文件读取请求体

需要用到ngx.req.get_body_file,从临时文件读取数据

--Nginx服务器中使用lua获取get或post参数
local request_method = ngx.var.request_method
local args = nil
local param = nil
local param2 = nil
--获取参数的值
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
param = args["param"]
param2 = args["param2"]
--升级版(能处理content-type=multipart/form-data的表单):
local function explode ( _str,seperator )
local pos, arr = , {}
for st, sp in function() return string.find( _str, seperator, pos, true ) end do
table.insert( arr, string.sub( _str, pos, st- ) )
pos = sp +
end
table.insert( arr, string.sub( _str, pos ) )
return arr
end
local args = {}
local file_args = {}
local is_have_file_param = false
local function init_form_args()
local receive_headers = ngx.req.get_headers()
local request_method = ngx.var.request_method
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
--判断是否是multipart/form-data类型的表单
if string.sub(receive_headers["content-type"],,) == "multipart/form-data;" then
is_have_file_param = true
content_type = receive_headers["content-type"]
--body_data可是符合http协议的请求体,不是普通的字符串
body_data = ngx.req.get_body_data()
--请求体的size大于nginx配置里的client_body_buffer_size,则会导致请求体被缓冲到磁盘临时文件里,client_body_buffer_size默认是8k或者16k
if not body_data then
local datafile = ngx.req.get_body_file()
if not datafile then
error_code =
error_msg = "no request body found"
else
local fh, err = io.open(datafile, "r")
if not fh then
error_code =
error_msg = "failed to open " .. tostring(datafile) .. "for reading: " .. tostring(err)
else
fh:seek("set")
body_data = fh:read("*a")
fh:close()
if body_data == "" then
error_code =
error_msg = "request body is empty"
end
end
end
end
local new_body_data = {}
--确保取到请求体的数据
if not error_code then
local boundary = "--" .. string.sub(receive_headers["content-type"],)
local body_data_table = explode(tostring(body_data),boundary)
local first_string = table.remove(body_data_table,)
local last_string = table.remove(body_data_table)
for i,v in ipairs(body_data_table) do
local start_pos,end_pos,capture,capture2 = string.find(v,'Content%-Disposition: form%-data; name="(.+)"; filename="(.*)"')
--普通参数
if not start_pos then
local t = explode(v,"rnrn")
local temp_param_name = string.sub(t[],,-)
local temp_param_value = string.sub(t[],,-)
args[temp_param_name] = temp_param_value
else
--文件类型的参数,capture是参数名称,capture2是文件名
file_args[capture] = capture2
table.insert(new_body_data,v)
end
end
table.insert(new_body_data,,first_string)
table.insert(new_body_data,last_string)
--去掉app_key,app_secret等几个参数,把业务级别的参数传给内部的API
body_data = table.concat(new_body_data,boundary)--body_data可是符合http协议的请求体,不是普通的字符串
end
else
args = ngx.req.get_post_args()
end
end
end

post请求体过大导致ngx.req.get_post_args()取不到参数体的问题的更多相关文章

  1. application/x-www-form-urlencoded 的contentType,POST数据内容过大,导致tomcat的request取不到参数

    如题, 可通过设置tomcat的connector的参数 server.xml中的connector中加上属性 maxPostSize="20971520" maxPostSize ...

  2. nginx的 ngx.var ngx.ctx ngx.req

    ngx.var 是获取 Nginx 的变量,需要经历字符串 hash.hash 表查找等过程. ngx.ctx 仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ct ...

  3. 【原创】大叔问题定位分享(36)openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil

    openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil This function returns nil if the request ...

  4. 转 listener.log文件过大导致oracle数据库连接非常慢

    数据库(31)  最近发现oracle数据库连接非常慢,sqlplus很快,用客户端就很慢,甚至会无响应. 然后服务器内存一下就飙升到了90%,不是表空间占满了,也不是数据库连接数占满了.重启还是一样 ...

  5. 分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据

    分享工作中遇到的问题积累经验 事务日志太大导致insert不进数据 今天开发找我,说数据库insert不进数据,叫我看一下 他发了一个截图给我 然后我登录上服务器,发现了可疑的地方,而且这个数据库之前 ...

  6. 解决父类加载iframe,src参数过大导致加载失败

    原文:解决父类加载iframe,src参数过大导致加载失败 <iframe src="*******.do?param=****" id="leftFrame&qu ...

  7. Oracle 监听器日志文件过大导致监听异常

    Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...

  8. Tomcat请求头过大

    今天开发反应Tomcat的请求头过大 <Connector port="8280" protocol="HTTP/1.1" connectionTimeo ...

  9. listener.log文件过大导致oracle假死

    /home/u01/oracle/product/11gr2/db_1/log/diag/tnslsnr/VM_179_95_centos/listener/trace/listener.log li ...

随机推荐

  1. java 跨域-利用jsonp

    Tomcat在 7.0.73, 8.0.39, 8.5.7 版本之前的可以用,不然后台报错 /** * 前端首页跳转后台 */ @RequestMapping("WebIndex.get&q ...

  2. 如何调试JDK源代码并查看局部变量值

    如下图: 按F5进入String.startsWith,如下: 点“Edit Source Lookup Path” 附加源代码,如下图: 附加上源代码后如下: 可以看到,当鼠标放在“prefix”上 ...

  3. HDU4565(SummerTrainingDay05-C 矩阵快速幂)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. HDU1074(KB12-D 状态压缩dp)

    Doing Homework Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  5. 2016计蒜之道复赛 百度地图的实时路况(Floyd 分治)

    题意 题目链接 Sol 首先一个结论:floyd算法的正确性与最外层\(k\)的顺序无关(只要保证是排列即可) 我大概想到一种证明方式就是把最短路树上的链拿出来,不论怎样枚举都会合并其中的两段,所以正 ...

  6. 网页CSS中*{margin:0; padding:0;}有什么用

    * 这东西叫“通配符”用来匹配页面上所有元素.*{margin:0; padding:0;} 像 2L 所说,body ,ul, li ,p,h1~h6,dd,dt 等……都有默认的margin 或p ...

  7. 如何扩展Linux虚拟内存文件系统

    由于ArcGIS GeoAnalystics Server和Raster Analytics Server大数据分析平台都是基于Spark分析平台的,其部署服务器除了要求具有高内存特点外,也需要确保相 ...

  8. 安卓逆向(一)--Smali基础

    安卓逆向(一)--Smali基础 标签(空格分隔): 安卓逆向 APK的组成 文件夹 作用 asset文件夹 资源目录1:asset和res都是资源目录但有所区别,见下面说明 lib文件夹 so库存放 ...

  9. Cookie、Session 和 Token区别

    1 Cookie.Session 和 Token 都是用来做持久化处理的,目的就是让客户端和服务端相互认识.Http 请求默认是不持久的没有状态的,谁也不认识谁.   2 Cookie: 是存放在客户 ...

  10. Loadrunner打开VU时候报错Critical error(cannot use Exceptiondialog)

    打开Loadrunner打开VU时候报错Critical error(cannot use Exceptiondialog) 卸载后,删掉注册表,重新安装,打开还是这样 怎么办呢 我男票告诉我,从开始 ...