Last active
April 27, 2021 22:37
-
-
Save nginx-gists/49e2f6f6b0a36ed9846dd63cddbd742a to your computer and use it in GitHub Desktop.
Harnessing the Power and Convenience of JavaScript for Each Request with the NGINX JavaScript Module
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 kvAccess(r) { | |
var log = `${r.variables.time_iso8601} client=${r.remoteAddress} method=${r.method} uri=${r.uri} status=${r.status}`; | |
r.rawHeadersIn.forEach(h => log += ` in.${h[0]}=${h[1]}`); | |
r.rawHeadersOut.forEach(h => log += ` out.${h[0]}=${h[1]}`); | |
return log; | |
} | |
export default { kvAccess } |
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
js_import conf.d/logging.js; # Load JavaScript code from here | |
js_set $access_log_headers logging.kvAccess; # Fill variable from JS function | |
log_format kvpairs $access_log_headers; # Define special log format | |
server { | |
listen 80; | |
root /usr/share/nginx/html; | |
access_log /var/log/nginx/access.log kvpairs; | |
} | |
# vim: syntax=nginx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a discussion of these files, see: Harnessing the Power and Convenience of JavaScript for Each Request with the NGINX JavaScript Module