Welcome! Log In Create A New Profile

Advanced

RE: Full logging

Reinis Rozitis
September 09, 2020 07:34AM
> 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

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

Full logging

Hans Liss September 09, 2020 05:12AM

RE: Full logging

Reinis Rozitis September 09, 2020 07:34AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 91
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready