Skip to content

Instantly share code, notes, and snippets.

@kaichao
Last active April 4, 2025 11:14
Show Gist options
  • Save kaichao/077340aa66e8cdf858577a93a9fe1b0d to your computer and use it in GitHub Desktop.
Save kaichao/077340aa66e8cdf858577a93a9fe1b0d to your computer and use it in GitHub Desktop.
nginx: Log complete request/response with all headers

1. switch nginx image to openresty/openresty

2. add the following to server/location (/etc/nginx/conf.d/default.conf)

   set $req_header "";
   set $resp_header "";
   header_filter_by_lua_block{ 
      local h = ngx.req.get_headers();
      for k, v in pairs(h) do
         ngx.var.req_header = ngx.var.req_header .. k.."="..v.." ";
      end
      local rh = ngx.resp.get_headers();
      for k, v in pairs(rh) do
         ngx.var.resp_header = ngx.var.resp_header .. k.."="..v.." ";
      end
   }

   lua_need_request_body on;
   set $resp_body "";
   body_filter_by_lua_block {
      local resp_body = string.sub(ngx.arg[1], 1, 1000)
      ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
      if ngx.arg[2] then
         ngx.var.resp_body = ngx.ctx.buffered
      end
   }

   # access_log  /dev/stdout log_req_resp;
   access_log   /var/log/nginx-access.log log_req_resp;

3. add the following to http (/usr/local/openresty/nginx/conf/nginx.conf)

   log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
      '"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" '
      '$request_time req_header:"$req_header" resp_header:"$resp_header" '
      'req_body:"$request_body" resp_body:"$resp_body"';   

individual headers

request headers:$http_<header> 
 sent headers: $sent_http_<header>
@Valeriyy
Copy link

Valeriyy commented Feb 3, 2025

Logging of responce header is not working with the Lua code, because error from nginx:
2025/02/03 15:23:35 [error] 6#6: *1 failed to run header_filter_by_lua*: header_filter_by_lua(nginx.conf:33):9: attempt to concatenate local 'v' (a table value) stack traceback:         header_filter_by_lua(nginx.conf:33):9: in main chunk while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET / HTTP/1.1", upstream: "https://10.211.81.171:8443/", host: "127.0.0.1:8080" 127.0.0.1 - - [03/Feb/2025:15:23:35 +0000] "GET / HTTP/1.1" 302 0 "-" "curl/7.88.1" "-" req_header: "user-agent=curl/7.88.1 accept=*/* host=127.0.0.1:8080 " req_body: "-" resp_header: "" resp_body: ""
It's need in formating, because in value may be array or hash. Here is solution: https://stackoverflow.com/a/66010794/7672941

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