Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
server {
set $response_body ''; #we must declare variables first, we cannot create vars in lua
set $error_request_body '';
set $error_response_body '';
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000) -- arg[1] contains a chunk of response content
ngx.ctx.buffered = string.sub((ngx.ctx.buffered or "") .. resp_body, 1, 1000)
if ngx.arg[2] then -- arg[2] is true if this is the last chunk
ngx.var.response_body = ngx.ctx.buffered
end
';
log_by_lua '
if ngx.var.status ~= "200" and ngx.var.status ~= "201" and ngx.var.status ~= "302" then
ngx.var.error_response_body = ngx.var.response_body
ngx.var.error_request_body = ngx.var.request_body
end
';
location /service-uri {
...
access_log /var/log/nginx/service-uri-access.log timing_with_error_body;
...
}
}
http {
...
log_format timing_with_error_body '$remote_addr $http_x_forwarded_for [$time_local] '
'"$request" $status $body_bytes_sent $request_time $upstream_response_time '
'"$http_referer" "$http_user_agent" "$error_request_body" "$error_response_body"';
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment