This configuration can be used to log all of the request headers presented by a client. We use the JavaScript (njs) module to iterate over each header, returning a list of JSON key-value pairs which are then included in a JSON log format.
js_include functions.js;
js_set $headers_json headers_to_json;
log_format json escape=none '{"timestamp":"$time_iso8601",'
'"client":"$remote_addr",'
'"uri":"$request_uri",'
'"status":$status,'
'"size":$bytes_sent,'
'"request headers":{$headers_json}}';
…
server {
listen 80;
access_log /var/log/nginx/access.log json;
…
}
Example log output:
$ tail -1 access.log | jq
{
"timestamp": "2018-10-04T22:51:20+01:00",
"client": "172.16.52.1",
"uri": "/api/f1/current/last",
"status": 200,
"size": 681,
"request headers": {
"host": "api.example.com",
"cache-control": "max-age=0",
"upgrade-insecure-requests": 1,
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-GB,en-US;q=0.9,en;q=0.8"
}
}
Dear lcrilly,
js_include functions.js;
I have an error nginx: [emerg] unknown directive "js_include"
Can you help me?
Thank you so much