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
    ';
posted @   白云辉  阅读(2212)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示