How to log all headers in nginx?

本小妞迷上赌 提交于 2019-12-30 03:40:08

问题


How do I log all the headers the client (browser) has sent in Nginx? I also want to log the response headers. Note that I am using nginx as reverse proxy.

After going through documentation, I understand that I can log a specific header, but I want to log all of the headers.


回答1:


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




回答2:


As @gauravphoenix said you need opnresty 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..": "..v..";")
  end
}

Inspect your error log.




回答3:


Based on @user1778602 response the set_by_lua can be used to set all headers into a variable to be consumed later at the log_format. The following sets all headers in the variable "$request_headers"

set_by_lua $request_headers '
  local h = ngx.req.get_headers()
  local request_headers_all = ""
  for k, v in pairs(h) do
    request_headers_all = request_headers_all .. ""..k..": "..v..";"
  end
  return request_headers_all
';


来源:https://stackoverflow.com/questions/24380123/how-to-log-all-headers-in-nginx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!