nginx 开启 response和header 日志记录
log_format json escape=json '{"remote_addr": "$remote_addr",'
'"@timestamp": "$time_iso8601",'
'"request_uri": "$request_uri",'
'"verb": "$request_method",'
'"httpversion": "$server_protocol",'
'"response": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"referrer": "$http_referer", '
'"user_agent": "$http_user_agent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"server_name": "$host",'
'"request_time": "$request_time",'
'"upstream_response_time": "$upstream_response_time",'
'"realpath_root": "$realpath_root",'
'"cookie": "$http_cookie",'
'"request_body": "$request_body",'
'"nginx_version": "$nginx_version",'
'"resp_header":"$resp_header",'
'"req_header":"$req_header",'
'"resp_body":"$resp_body",'
'"scheme": "$scheme"}';
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] and ngx.status ~= 200 then
ngx.var.resp_body = ngx.ctx.buffered
end
';
set $req_header "";
set $resp_header "";
header_filter_by_lua '
local h1 = ngx.req.get_headers()
for k1, v1 in pairs(h1) do
ngx.var.req_header=ngx.var.req_header..k1..": "..v1
end
local h = ngx.resp.get_headers()
for k, v in pairs(h) do
ngx.var.resp_header=ngx.var.resp_header..k..": "..v
end
';
作者:百里求一
出处:http://www.cnblogs.com/bergus/
我的语雀: https://www.yuque.com/barry.bai
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。