Skip to content

Instantly share code, notes, and snippets.

@nginx-gists
Last active April 27, 2021 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nginx-gists/49e2f6f6b0a36ed9846dd63cddbd742a to your computer and use it in GitHub Desktop.
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
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 }
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
@nginx-gists
Copy link
Author

nginx-gists commented Mar 15, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment