• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

openresty(nginx+lua)中获取不到post数据,ngx.req.get_body_data返回nil

This function returns nil if

  • the request body has not been read,
  • the request body has been read into disk temporary files,
  • or the request body has zero size.

打开nginx调试日志

error_log /var/log/nginx/error.log debug;

发现如下日志:

2019/07/26 10:41:43 [warn] 319#319: *3041588 a client request body is buffered to a temporary file /usr/local/openresty/nginx/client_body_temp/0000001574, client: 192.168.0.2, server: localhost, request: "POST /test HTTP/1.1", host: "192.168.0.1"

可见是因为body太大,被缓冲到临时文件中,此时应该使用ngx.req.get_body_file

fix代码

function read_from_file(file_name)
  local f = assert(io.open(file_name, "r"))
  local string = f:read("*all")
  f:close()
  return string
end

ngx.req.read_body()
local body = ngx.req.get_body_data()
if (body == nil) then
  local body_file = ngx.req.get_body_file()
  if body_file then
    body = read_from_file(body_file)
  else
    ngx.status = 500
    ngx.say("error")
    return
  end
end

 

参考:
https://github.com/openresty/lua-nginx-module#ngxreqget_body_data


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua中分割字符串发布时间:2022-07-22
下一篇:
cocos2dxlua中异步加载网络图片,可用于显示微信头像发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap