开发者社区 问答 正文

nginx 怎么记录post请求数据日志?:配置报错 

nginx  怎么记录post请求数据日志?windows下的环境,需要记录的接口和nginx都在win下,现在想日志能记录下post提交过来的数据,按照网上的方法都无效。

展开
收起
kun坤 2020-05-31 23:57:03 1188 分享 版权
1 条回答
写回答
取消 提交回答
  • 使用过ngx_lua的方式 记录请求信息 以及响应信息  首先 nginx 是不会读取http请求的body的 ,下面说明的  是我所知道的一个办法  存手打  代码有部分复制我写的。。 首先用lua在nginx内部的做一个小代理转发

            location / {             content_by_lua_block {                 local method_name = ngx.req.get_method()  -- 获得请求方法                 local get_uri_args = ngx.req.get_uri_args  -- 获得get参数的方法                 local args = get_uri_args()   -- 获得get参数                 local res                 if method_name == 'GET' then                     res = ngx.location.capture('/sub', {method = ngx.GET, ['args'] =  args, ctx = {myuri = ngx.var.uri, my_remote_addr = ngx.var.remote_addr}})                 elseif method_name == 'POST' or method_name == 'PUT' then                     ngx.req.read_body() -- 读取请求body                     res = ngx.location.capture('/sub', {always_forward_body = true, ['args'] =  args, ctx = {myuri = ngx.var.uri, my_remote_addr = ngx.var.remote_addr}})                 end                -- 这是子请求返回的header                  local header = res.header                 -- 设置响应header                 for key in pairs(header) do                     ngx.header[key] = header[key]                 end                -- 下面两个函数方法可以获得post参数   https://github.com/openresty/lua-nginx-module  这个地址有更详细的文档  文档里面提供了可以操作数据库等方法 你可以用做存储数据                 -- ngx.req.get_body_data()  -- post 参数  这个值都是字符串  存在\r\n这些                 --  ngx.req.get_post_args() -- 或者这个方法   但是 需要注意的是  MIME type 需要  application/x-www-form-urlencoded                 ngx.status = res.status   -- 设置状态码  跟子请求一样                 ngx.say(res.body)  -- 输出子请求的响应body                ngx.exit( res.status)                  }         }

    这是内部的location  是上面content_by_lua_block块  ngx.location.capture  转发过来的

    location = /sub {             internal;             set_by_lua_block $myuri {                 return ngx.ctx.myuri             }             set_by_lua_block $my_remote_addr {                 return ngx.ctx.my_remote_addr             }             try_files $myuri $myuri/ /index.php?$query_string;         }   如果你只想要记录post的请求话  可以不用的上面的子请求方式   // 其实记录响应信息也可以不用。。 但是我做的时候太麻烦了  线上没用 我就没贴出来 下面的只记录就请求信息的方式简介 在log_by_lua_*  阶段 也可以获取请求信息并且记录 在nginx配置文件中添加 lua_need_request_body on;  # 具体原因 https://github.com/iresty/nginx-lua-module-zh-wiki#lua_need_request_body  查看 location / {     log_by_lua_block {             -- ngx.req.get_body_data()  -- post 参数  这个值都是字符串  存在\r\n这些       --  ngx.req.get_post_args() -- 或者这个方法   但是 需要注意的是  MIME type 需要  application/x-www-form-urlencoded     } } 但是需要注意 log_by_log 阶段无法使用socket相关的API  所以无法插入数据库等操作  所以可以用几种思路来 1、 https://github.com/p0pr0ck5/lua-resty-logger-socket  具体使用看这个文档 2、 我使用的是把信息保存在ngx_lua 的全局共享内存中  操作方式类似redis,然后在其他阶段执行, 具体使用API还是可以看文档    最后你使用的是windows  所以可以使用已经打包好的openresty win版  https://openresty.org/download/openresty-1.13.6.1-win32.zip   openresty 其实使用起来跟nginx是一样的  ######你不说大家怎么知道你试过哪些方法....######通过lua的方式获取到了记录,但是不知道怎么返回的数据突然就有问题了,提示返回参数错误######你的现象说的不具体啊 很难帮你想

    2020-05-31 23:57:07
    赞同 展开评论