Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
nginx lua获取响应体_Nginx_Lua - Fatal编程技术网

nginx lua获取响应体

nginx lua获取响应体,nginx,lua,Nginx,Lua,我需要得到响应体,例如从url的响应html代码,我使用以下代码 location /configure/result.php { log_by_lua_block { ngx.log(ngx.ERR, "REQUEST capturing started") json = require "json" function getval(v, def) if v == nil then return def end

我需要得到响应体,例如从url的响应html代码,我使用以下代码

location /configure/result.php {

  log_by_lua_block  {
    ngx.log(ngx.ERR, "REQUEST capturing started")

    json = require "json"

    function getval(v, def)
      if v == nil then
         return def
      end
      return v
    end

    local data = {
      request={},
      response={}
    }
    local req = data.request
    local resp = data.response

    req["host"] = ngx.var.host
    req["uri"] = ngx.var.uri
    req["headers"] = ngx.req.get_headers()
    req["time"] = ngx.req.start_time()
    req["method"] = ngx.req.get_method()
    req["get_args"] = ngx.req.get_uri_args()
    req["post_args"] = ngx.req.get_post_args()
    req["body"] = ngx.var.request_body

    content_type = getval(ngx.var.CONTENT_TYPE, "")

    resp["headers"] = ngx.resp.get_headers()
    resp["status"] = ngx.status
    resp["duration"] = ngx.var.upstream_response_time
    resp["time"] = ngx.now()
    resp["body"] = ngx.var.response_body -- Problem Here

    ngx.log(ngx.CRIT, json.encode(data));
  }
}
但它不会记录从该url接收到的响应数据,例如,已处理的源代码如何获取响应数据


我的想法是获取响应数据,然后使用regEx从源代码中读取specifiq值,然后执行x-y

我现在无法测试这一点,但可能在请求处理的日志阶段响应不可访问

您可以尝试使用以下表格通过\u lua\u块获取
正文\u过滤器\u中的响应数据:

然后在
log\u by\u lua\u块中使用它
块:

    log_by_lua_block {
        ...
        resp["body"] = ngx.ctx.response_body
        ngx.log(ngx.CRIT, json.encode(data))
    }
(请注意,这只是一个猜测,请让我知道这是否有效)

    log_by_lua_block {
        ...
        resp["body"] = ngx.ctx.response_body
        ngx.log(ngx.CRIT, json.encode(data))
    }