Saving request headers into log in nginx

опубликовано

Тэги: nginx js

Create /etc/nginx/utils.js with necessary functions:

const headersAsJson = (req) => {
    return JSON.stringify(req.headersIn)
}

export default {
    headersAsJson
}

Update nginx.conf:

...
http {
    ...
    js_import utils.js;
    js_set $headers_as_json utils.headersAsJson;
    ...
    log_format main_with_headers "$request_id\t...\t$headers_as_json";
    ...
}

And apply this format to server:

server {
    ...
    access_log /path/to/access.log main_with_headers;
    ...
}