$request_id
The $request_id variable contains a unique identifier for each request processed by NGINX. — NGINX Core (HTTP)
Description
The $request_id variable provides a unique identifier for each incoming request to the NGINX server. This identifier is useful for request tracking and logging, particularly in distributed systems or microservices architectures, where correlating logs across multiple services can aid in debugging and monitoring. By default, if a request does not contain an explicit `X-Request-ID` header, NGINX generates and assigns a UUID (Universally Unique Identifier) as the request ID. This means that every request handled by NGINX can be tracked regardless of whether the client has specified this header. When the request reaches an NGINX server, it might undergo several processing steps (like authentication, access rules, etc.), and the $request_id remains consistent throughout those processes. It's important to enable this variable by using the `request_id` module, which may not be present in minimal NGINX builds. In cases where external systems or services are involved (such as API gateways or load balancers), passing the request ID through the request headers ensures consistent tracking across the entire request lifecycle. Typical values of $request_id are formatted as UUID strings, ensuring global uniqueness. For instance, a sample output could look like `3e4e989c-45c6-4de4-b7d6-2b4ebc4fc417`. This makes it extremely helpful for identifying and correlating log entries pertaining to the same request across different system components or layers.
Config Example
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$_http_referer" "$_http_user_agent" "$request_id"';
daily_log {
access_log /var/log/nginx/access.log main;
proxy_set_header X-Request-ID $request_id;
}
}Subsystem
httpCacheable
YesContexts
http, server, locationEnsure that the request_id module is enabled in your NGINX build, as some minimal builds may not include it.
The variable will not be available until the request processing phase where it is assigned, so using it in early phases (like server block) may yield unexpected results.
Not all upstream services may support or be able to log this request ID unless explicitly configured.