Skip to content

Instantly share code, notes, and snippets.

@virusdefender
Last active November 22, 2023 02:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save virusdefender/984c2a304001d8de20049301b478700a to your computer and use it in GitHub Desktop.
Save virusdefender/984c2a304001d8de20049301b478700a to your computer and use it in GitHub Desktop.
log request and response header and body with openresty | 使用 openresty 记录请求和响应 http 头和 body
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
error_log stderr info;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
#lua_need_request_body on;
set $resp_body "";
set $req_body "";
access_by_lua '
ngx.req.read_body();
';
body_filter_by_lua '
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. ngx.arg[1]
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';
log_by_lua '
ngx.log(ngx.INFO, ngx.req.raw_header());
ngx.log(ngx.INFO, ngx.status);
ngx.log(ngx.INFO, ngx.req.get_body_data());
local h = ngx.req.get_headers(30)
for k, v in pairs(h) do
ngx.log(ngx.INFO, v);
end
ngx.log(ngx.INFO, ngx.var.resp_body);
';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment