Full logging

Reinis Rozitis r at roze.lv
Wed Sep 9 11:32:02 UTC 2020


> I need a HTTP proxy that can handle requests for a single upstream server,
> but also log request headers, raw request body, response headers and raw
> response body for each request. Preferably this should be logged to a
> separate daily logfile (with date-stamped filename), with timestamps, but the
> exact format isn't important.


By default nginx can log the request body ( http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_body ),
for the response body an option is to use the lua module https://github.com/openresty/lua-nginx-module (either compile yourself or use Openresty)  

But it might be way more simple just to plug a proxy (developed for exact purposes) between nginx and upstream. Like for example https://mitmproxy.org/



I have an old test example (not sure if still working) which shows the idea, but maybe there is a more elegant way to do it:

http {

   log_format bodylog '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time "$req_headers" "$req_body" "$resp_body"';


sevrver {

        lua_need_request_body on;

        set $resp_body "";
        set $req_body "";
        set $req_headers "";

        rewrite_by_lua_block {
            local req_headers = "Headers: ";
            ngx.var.req_body = ngx.req.get_body_data();
            local h, err = ngx.req.get_headers()
            for k, v in pairs(h) do
                req_headers = req_headers .. k .. ": " .. v .. "\n";
            end

            ngx.var.req_headers = req_headers;
        }

        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
        ';
     }
}



rr



More information about the nginx mailing list