Last active
November 22, 2023 02:16
-
-
Save virusdefender/984c2a304001d8de20049301b478700a to your computer and use it in GitHub Desktop.
log request and response header and body with openresty | 使用 openresty 记录请求和响应 http 头和 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
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