Skip to content

Instantly share code, notes, and snippets.

@psaussure
Forked from morhekil/nginx.conf
Created January 4, 2017 19:12
Show Gist options
  • Save psaussure/b53a3c63b92d13c9b0fd77f41b85eacb to your computer and use it in GitHub Desktop.
Save psaussure/b53a3c63b92d13c9b0fd77f41b85eacb to your computer and use it in GitHub Desktop.
Full request/response body logging in nginx
http {
log_format bodylog '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time '
'<"$request_body" >"$resp_body"';
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] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
}
server {
listen 1.2.3.4;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
access_log /var/log/nginx/server.log bodylog;
}
}
@psaussure
Copy link
Author

Useful comments:

ngx.log(ngx.ERR, ngx.var.resp_body, ngx.var.request_body )

shoeb751:
just wondering why you did local resp_body = string.sub(ngx.arg[1], 1, 1000) instead of local resp_body = string.sub(ngx.arg[1], 1, -1) or perhaps local resp_body = ngx.arg[1] for that matter?
The last two approaches makes sure that you do not lose the response body. Which happens in your case where it gets truncated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment