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 @ 2021-06-11 12:52  白云辉  阅读(2186)  评论(0编辑  收藏  举报