如何将整个响应数据保存到 nginx error.log?
How to save whole response data to nginx error.log?
我想获取所有请求和响应的详细信息,我正在考虑将它们保存到 ngx.log。
我使用这样的代码来保存它们,我想从响应主体中获取5000个字符的长度,但是在error.log文件中,它只为每个响应保存了一部分响应数据,这比 5000 短得多。
body_filter_by_lua_block
{
local resp_body = string.sub(ngx.arg[1], 1, 5000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
}
log_by_lua_block{
local data = {response={}, request={}}
local req = ngx.req.get_headers()
req.accessTime = os.date("%Y-%m-%d %H:%M:%S")
data.request = req
local resp = data.response
resp.headers = ngx.resp.get_headers()
resp.status = ngx.status
resp.duration = ngx.var.upstream_response_time
resp.body = ngx.var.resp_body
ngx.log(ngx.NOTICE,"from log pharse:", json.encode(data));
}
请帮我解释一下,以及如何更改任何配置以保存整个响应数据。或任何其他更适合保存请求和响应详细信息的建议。
谢谢!
那是因为ngx.arg[1]只是一个数据块,你必须将字符串长度与缓冲区进行比较,就像这样:
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. ngx.arg[1]
if ngx.arg[2] then
ngx.var.resp_body = string.sub(ngx.ctx.buffered, 1, 5000)
end
#client_body_buffer_size 5000;
body_filter_by_lua_block{
local resp_body = string.sub(ngx.arg[1], 1, 5000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.log(ngx.INFO, ngx.ctx.buffered)
end
}
我已经改变了一些,我用上面的方法在 nginx(openresty) 日志中记录响应
我想获取所有请求和响应的详细信息,我正在考虑将它们保存到 ngx.log。
我使用这样的代码来保存它们,我想从响应主体中获取5000个字符的长度,但是在error.log文件中,它只为每个响应保存了一部分响应数据,这比 5000 短得多。
body_filter_by_lua_block
{
local resp_body = string.sub(ngx.arg[1], 1, 5000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
}
log_by_lua_block{
local data = {response={}, request={}}
local req = ngx.req.get_headers()
req.accessTime = os.date("%Y-%m-%d %H:%M:%S")
data.request = req
local resp = data.response
resp.headers = ngx.resp.get_headers()
resp.status = ngx.status
resp.duration = ngx.var.upstream_response_time
resp.body = ngx.var.resp_body
ngx.log(ngx.NOTICE,"from log pharse:", json.encode(data));
}
请帮我解释一下,以及如何更改任何配置以保存整个响应数据。或任何其他更适合保存请求和响应详细信息的建议。 谢谢!
那是因为ngx.arg[1]只是一个数据块,你必须将字符串长度与缓冲区进行比较,就像这样:
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. ngx.arg[1]
if ngx.arg[2] then
ngx.var.resp_body = string.sub(ngx.ctx.buffered, 1, 5000)
end
#client_body_buffer_size 5000;
body_filter_by_lua_block{
local resp_body = string.sub(ngx.arg[1], 1, 5000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.log(ngx.INFO, ngx.ctx.buffered)
end
}
我已经改变了一些,我用上面的方法在 nginx(openresty) 日志中记录响应