gpt4 book ai didi

nginx - 如何将整个响应数据保存到nginx error.log?

转载 作者:行者123 更新时间:2023-12-02 03:32:19 27 4
gpt4 key购买 nike

我想获取所有请求和响应详细信息,并且我正在考虑将它们保存到 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] 只是一 block 数据,你必须将字符串长度与缓冲区进行比较,如下所示:

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

关于nginx - 如何将整个响应数据保存到nginx error.log?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51492910/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com