$bytes_received

The $bytes_received variable tracks the total number of bytes received from clients in NGINX's stream module. — NGINX Core (Stream)

$bytes_received NGINX Core (Stream)

Description

In NGINX's stream module, the variable $bytes_received is dynamically set to reflect the number of bytes received from a client for the duration of a given connection. It helps in monitoring the amount of data a server has received through TCP or UDP data streams. Each time data is received from the client, this variable is updated accordingly and can be leveraged for logs or performance metrics. Consequently, $bytes_received essentially counts all bytes, including headers and payloads, making it a great tool for bandwidth analytics. This variable is initialized at the start of a connection and is available for use within various contexts related to active connections. For instance, within log formats, monitoring modules, or for implementing rate limiting based on the total bytes received. Typical values of $bytes_received can vary widely depending on the application but can usually scale from a few bytes to several megabytes or more during heavy loads, contingent on the type of traffic and configuration settings of the NGINX stream server.

Config Example

stream {
    server {
        listen 12345;
        access_log /var/log/nginx/stream_access.log "$remote_addr - $bytes_received bytes received";
    }
}

Subsystem

stream

Cacheable

Yes

Contexts

stream, server, if

Ensure the stream module is enabled in NGINX, as this variable is specific to it.

Be cautious when using $bytes_received in shared memory contexts because its value may reset when connections are closed.

Remember that this variable counts bytes only from the moment a connection is established until it is terminated.