Last active
December 16, 2019 20:10
-
-
Save enginyoyen/d6e3ae15f055209066e0 to your computer and use it in GitHub Desktop.
[Nginx Body Filter] #nginx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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