Skip to content

Instantly share code, notes, and snippets.

@shoeb751
Forked from morhekil/nginx.conf
Last active April 14, 2021 11:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoeb751/84be11193ac1f3514c5eb4d70a8ba973 to your computer and use it in GitHub Desktop.
Save shoeb751/84be11193ac1f3514c5eb4d70a8ba973 to your computer and use it in GitHub Desktop.
Full request/response body logging in nginx
http {
server {
listen 1.2.3.4;
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"';
set $resp_body "";
lua_need_request_body on;
body_filter_by_lua '
local resp_body = ngx.arg[1]
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
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;
}
}
}
@mallikharjunrao
Copy link

mallikharjunrao commented Aug 22, 2017

@shoeb751 getting the exception: "set" directive is not allowed here in ~ /nginx/conf/nginx.conf
any solution? this configuration used for default nginx.conf also help me how to configure for multihost config and seperate logs for l hosts.

Thank you very much.

@thinkingerrol
Copy link

@mallikharjunrao it is probably too late to answer you after 4 years but for the record: "set" directive can only be used in "server", "location" and "if" blocks. This has already been fixed in this gist.

https://stackoverflow.com/questions/26874828/set-directive-is-not-allowed-here
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#set

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