Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all headers in nginx?

Tags:

logging

nginx

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.

like image 252
gauravphoenix Avatar asked Jun 24 '14 06:06

gauravphoenix


People also ask

Can nginx log headers?

I don't know if it's a new feature since the OP asked the question, but you can simply turn on debug level logging, and NGINX will log all request headers (plus a whooole lot more) to the error log. I wouldn't leave this enabled, since (1) it may log sensitive data and (2) it will fill up your disk very quickly.

How do I get nginx logs?

Configure NGINX access log By default, the access log is located at /var/log/nginx/access. log , and the information is written to the log in the predefined combined format. You can override the default settings and change the format of logged messages by editing the NGINX configuration file ( /etc/nginx/nginx.

How do I enable logging in nginx?

Enabling the error log The NGINX error log should be enabled by default. However, if this is not the case, you can enable it manually in the relevant NGINX configuration file (either at the http , server , or location levels) using the error_log directive. error_log /var/log/nginx/error.

What is nginx default log format?

The default log format in nginx is called "combined".


2 Answers

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

like image 158
gauravphoenix Avatar answered Oct 16 '22 02:10

gauravphoenix


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.

like image 33
user1778602 Avatar answered Oct 16 '22 00:10

user1778602