$connection_requests

The $connection_requests variable returns the number of requests received on the current connection. — NGINX Core (HTTP)

$connection_requests NGINX Core (HTTP)

Description

The $connection_requests variable in NGINX is utilized to track the number of requests that have been processed during the life of a single connection. It is incremented every time a new request is made on that connection, starting from zero when the connection is first established. Each request – whether it's an initial request or subsequent requests triggered by keep-alive – will increase this count. This variable is particularly useful for monitoring and understanding request patterns, optimizing connection reuse, and debugging performance issues related to connection handling. In NGINX, the lifecycle of the $connection_requests variable begins when a new connection is established. For each incoming request on that connection, the server will increment the variable until the connection is closed or the request processing has been completed. Typical values for this variable could start from 1 for a single request connection and increase accordingly with multiple requests made by the client within that same connection. In scenarios where connections are kept alive, this variable allows web administrators to analyze how effectively keep-alive connections manage multiple requests compared to opening a new connection for each request. Given that this variable reflects the number of requests, it can be instrumental in configurations aimed at performance tuning or detailed request logging, where the insights gained from monitoring the variable could lead to improvements in resource allocation and response times. Logging this variable can provide valuable insights into the behavior of clients and their request patterns, making it easier to identify potential bottlenecks or increase efficiency.

Config Example

log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$connection_requests"';

access_log /var/log/nginx/access.log custom_format;

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

The variable is only applicable when keep-alive is utilized; otherwise, it will always default to 1 for single requests.

Be cautious of logging verbosity; logging the $connection_requests variable in high-traffic scenarios may affect performance.

Ensure server blocks and locations are correctly configured to make use of keep-alive; otherwise the variable increments will not reflect multiple requests.