$body_bytes_sent

The $body_bytes_sent variable in NGINX contains the number of bytes sent to the client in the response body, excluding the response headers. — NGINX Core (HTTP)

$body_bytes_sent NGINX Core (HTTP)

Description

The $body_bytes_sent variable is a significant metric used in NGINX logging and performance monitoring. It captures the total amount of data sent to the client, specifically the bytes that are transmitted in the response body. This variable is set during the processing of each request, once the response body has been completed and sent. It is important to note that this variable does not count the headers sent to the client, only the body content. This variable becomes particularly useful in scenarios involving performance evaluation and bandwidth calculations. As the handling of HTTP requests progresses, NGINX maintains an accumulator for the transmitted bytes, which culminates in the final value for $body_bytes_sent. Typical values can range from 0 (for cases where the request results in an error or no body content is sent) to any arbitrary size based on the nature of the response—small for simple HTML pages or large for file downloads or media streaming. In practice, the value of $body_bytes_sent can help administrators identify traffic patterns, user engagement with content, and potential areas of optimization within their applications. By logging this variable, one can analyze bandwidth usage as well as diagnose issues related to slow responses, by correlating the bytes transmitted with other variables such as response time and request rate.

Config Example

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

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

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Make sure that the variable is accessed after the response body has been set or transmitted, otherwise it may return unexpected values like 0.

Remember that $body_bytes_sent does not include headers; if you require total bytes sent (including headers), you'll need to account for that separately.