$request_length

The variable $request_length returns the total size of the client request body in bytes. — NGINX Core (HTTP)

$request_length NGINX Core (HTTP)

Description

The $request_length variable in NGINX holds the byte count of the client request body, which includes the headers as well as any data sent by the client as part of the request (typically in POST requests). This variable is particularly useful for monitoring the size of incoming requests and for applying rate limiting based on request size. NGINX sets the $request_length value when it parses the incoming request. It is generally populated during the processing of requests in both server and location contexts. The size recorded is the total length of the request, counted in bytes. Typical values for this variable vary greatly depending on the nature of the application; common sizes could range from 0 bytes for simple GET requests with no body, to several kilobytes for complex POST requests containing forms or file uploads. This variable can help in situations where you might want to implement security measures to reject overly large request bodies or to keep track of the bandwidth being consumed by client requests. Administrators can thus set limits based on this variable to optimize server performance and manage resources effectively.

Config Example

http {
    server {
        listen 80;
        location /upload {
            client_max_body_size 1M;
            if ($request_length > 1048576) {
                return 413;
            }
        }
    }
}

Subsystem

http

Cacheable

No

Contexts

http, server, location, if

The $request_length variable is only applicable for POST requests; for GET requests, it typically returns 0.

Misinterpretation of the variable could lead to erroneously applying restrictions based on request GET method where no body exists.