http-headers X-Request-ID nginx


Reverse proxies can detect if a client provides a X-Request-ID header, and pass it on to the backend server. If no such header is provided, it can provide a random value.

map $http_x_request_id $reqid {                                                 
    default   $http_x_request_id;                                               
    ""        $request_id;                                                      

The code above stores the Request ID in the variable $reqid from where it can be subsequently used in logs.

log_format trace '$remote_addr - $remote_user [$time_local] "$request" '        
                 '$status $body_bytes_sent "$http_referer" "$http_user_agent" ' 
                 '"$http_x_forwarded_for" $reqid';                              

It should also be passed on to the backend services

    location @proxy_to_app {
        proxy_set_header X-Request-ID $reqid;
        proxy_pass   http://backend;
        access_log /var/log/nginx/access_trace.log trace;