How to log all headers in nginx? How to log all headers in nginx? nginx nginx

How to log all headers in nginx?


After much research, I can conclude that it is not possible out of the box.

Update- you can use openresty which comes with Lua. Using Lua one can do pretty cool things, including logging all of the headers to say, Redis or some other server


As @gauravphoenix said you need openresty which comes with Lua. See https://github.com/openresty/lua-nginx-module/ for installing it. Once it's running then add in nginx

header_filter_by_lua_block {  local h = ngx.req.get_headers()  for k, v in pairs(h) do    ngx.log(ngx.ERR, "Got header "..k..": "..toString(v)..";")  end}

Inspect your error log.


Based on @user1778602’s response the set_by_lua_block can be used to set all headers into a variable to be consumed later at the log_format (see more details in this answer).

set_by_lua_block $request_headers{  local h = ngx.req.get_headers()  local request_headers_all = ""  for k, v in pairs(h) do    local rowtext = ""    rowtext = string.format("[%s %s]\n", k, v)    request_headers_all = request_headers_all .. rowtext  end  return request_headers_all}

The update your log_format (be sure to use the new format in e.g. access_log):

log_format myformat escape=none "$request_headers"

PS: Beware of logging PII data may be violating GDPR :)