$sent_http_content_length

The $sent_http_content_length variable returns the value of the Content-Length header sent in the HTTP response. — NGINX Core (HTTP)

$sent_http_content_length NGINX Core (HTTP)

Description

When NGINX processes a request, it gathers various response headers that will be sent back to the client. The $sent_http_content_length variable specifically captures the Content-Length header that indicates the size of the response body in bytes. This variable is set during the request processing phase and will typically contain a numerical value representing the length of the response that is generated by the server, or it may be empty if the response length is not known or if chunked transfer encoding is used instead of a concrete content length. The Content-Length header is important for HTTP/1.1 communications, as it allows the client to know exactly how many bytes to expect in the response body. In scenarios where dynamic content is generated, NGINX might not determine the content length until the body is completely processed. Therefore, using this variable can help in scenarios where specific handling of response length is required, such as conditional logging or response modification based on content size.

Config Example

server {
    listen 80;
    location / {
        add_header X-Content-Length $sent_http_content_length;
        proxy_pass http://backend;
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

If the response uses chunked encoding, $sent_http_content_length will be empty.

Ensure that the variable is accessed after the response headers have been set to avoid unexpected results.