我正在尝试打印响应数据以用于开发/调试目的。我找不到类似的问题或任何相关信息。如果您找到了,请添加评论。
答案1
用于body_filter_by_lua
将请求体赋值给 nginx 变量,下面是示例:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time req_body:"$request_body" resp_body:"$resp_body"';
server {
listen 8082;
access_log logs/access.log log_req_resp;
lua_need_request_body on;
set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
location / {
echo "Hello World!";
}
}
}
答案2
使用 ngx_lua 模块
像这样
body_filter_by_lua 'ngx.log(ngx.CRIT,ngx.arg[1])';
在右边location