Skip to content

Instantly share code, notes, and snippets.

@danydodson
Last active November 15, 2022 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danydodson/9a296de2777a1d8ddcb335a5da14c715 to your computer and use it in GitHub Desktop.
Save danydodson/9a296de2777a1d8ddcb335a5da14c715 to your computer and use it in GitHub Desktop.
nginx header logs
# Load JavaScript code from here
js_include conf.d/header_logging.js;
# Fill variable from JS function
js_set $access_log_with_headers kvAccessLog;
# Define special log format
log_format kvpairs $access_log_with_headers;
server {
listen 80;
access_log /var/log/nginx/access_headers.log kvpairs;
location / {
proxy_pass http://www.example.com;
}
}
function kvHeaders(headers, parent) {
var kvpairs = ""
for (var h in headers) {
kvpairs += " " + parent + "." + h + "="
if (headers[h].indexOf(" ") == -1) {
kvpairs += headers[h]
} else {
kvpairs += "'" + headers[h] + "'"
}
}
return kvpairs
}
function kvAccessLog(r) {
var log = r.variables.time_iso8601 // NGINX JavaScript can access all variables
log += " client=" + r.remoteAddress // Property of request object
log += " method=" + r.method // "
log += " uri=" + r.uri // "
log += " status=" + r.status // Property of response object
log += kvHeaders(r.headersIn, "in") // Send request headers object to function
log += kvHeaders(r.headersOut, "out") // Send response headers object to function
return log
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment