$pid
The $pid variable returns the process ID (PID) of the NGINX worker process handling the current request. — NGINX Core (HTTP)
Description
The $pid variable in NGINX provides the process identifier (PID) of the worker process that is currently processing a client request. This variable is particularly useful for debugging and logging purposes, allowing administrators to identify which worker process is handling specific requests. The PID is initialized when the NGINX worker processes start and remains constant for the lifespan of the worker process. The value of $pid is numeric and reflects the system-assigned PID of the respective worker, which can be useful in scenarios where logging detailed information about process handling is necessary. Since NGINX can spawn multiple worker processes, each may be handling different requests concurrently. The $pid variable helps in correlating logs with specific PIDs, thus facilitating better diagnostics and monitoring. Typically, $pid values start from a defined number and increment for each new process spawned by the operating system. Users can utilize this variable in customized logging formats to provide insights on which processes are serving traffic, or when diagnosing performance bottlenecks or issues with individual worker processes.
Config Example
log_format custom_log '$remote_addr - $remote_user [$time_local] "$request" ' '"$status $body_bytes_sent "$http_referer" "-" "$http_user_agent" 'Ver: $nginx_version Pid: $pid'; access_log /var/log/nginx/access.log custom_log;
Subsystem
httpCacheable
YesContexts
http, server, location, ifThe $pid variable will only show relevant information if used within a supported context, such as inside a log or rewrite block.
It will reflect the PID of the current worker process, which may not correspond to the master process, potentially leading to confusion in environments with multiple processes.