Last active
November 15, 2022 05:49
-
-
Save danydodson/9a296de2777a1d8ddcb335a5da14c715 to your computer and use it in GitHub Desktop.
nginx header logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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