$proxy_internal_body_length

$proxy_internal_body_length returns the length of the request body received from a proxied server internally. — NGINX Core (HTTP)

$proxy_internal_body_length NGINX Core (HTTP)

Description

The `$proxy_internal_body_length` variable in NGINX is utilized within the context of proxying between servers, specifically when NGINX acts as a reverse proxy. This variable keeps track of the size of the body of the request that has been received from an upstream server. The value of this variable is set after NGINX processes the request coming from the upstream server, generally when the request is being forwarded or when a response is being received. This variable is particularly useful in configurations where the size of the body content needs to be monitored or manipulated. For example, if you're implementing content filtering, logging, or conditional processing based on body size, `$proxy_internal_body_length` provides an immediate and accurate way to retrieve the length of the incoming body data. The typical values for this variable will vary depending on the request and response scenarios, but it generally reflects the number of bytes present in the request body. In this scenario of proxying, when NGINX processes a request, it calculates the body length derived from the proxied content and sets it to this variable before handling further operations or directives where the size might be relevant.

Config Example

location /proxy {
    proxy_pass http://backend;
    access_log /var/log/nginx/proxy.log "$proxy_internal_body_length";
}

Subsystem

http

Cacheable

No

Contexts

http, server, location, if

This variable is only meaningful within the context of a proxied request and may be empty or zero if accessed outside that context.

Be cautious of expecting a non-zero value when the upstream server sends an empty body; it will be set to zero in such cases.

If using `$proxy_internal_body_length` in conditional processing, ensure that it is accessed after the request body has been processed.