$request_body_file

The $request_body_file variable contains the path to a temporary file holding the request body when the body is too large to fit in memory. — NGINX Core (HTTP)

$request_body_file NGINX Core (HTTP)

Description

The $request_body_file variable is used in scenarios where an HTTP request body exceeds NGINX's buffer size limitations. When a request is made, NGINX reads the request body; if the body is larger than a predefined size, it saves the body to a temporary file on the server filesystem. This variable then holds the file path of that temporary file, allowing further processing of the request body. The variable is primarily set during the request processing phase, specifically when any of the nucleus handler, such as the proxy or fastcgi modules, decide that the request body should not be processed entirely in memory. Typical values of this variable would include file paths on the filesystem, such as `/tmp/nginx_request_body_XXXXXX`. The temporary file is typically deleted after the request is completed or after it is processed, thereby managing server disk space efficiently. In configurations where handling large payloads (like file uploads) is common, configuring this request body handling appropriately, including buffer sizes, is crucial to ensure optimal performance and avoid server overloads.

Config Example

server {
    client_max_body_size 10M;
    location /upload {
        proxy_pass http://backend;
        proxy_request_buffering on;
        # Accessing the request body file:
        if ($request_body_file) {
            log_format custom '$remote_addr $request_body_file';
            access_log /var/log/nginx/custom.log custom;
        }
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location

Make sure the client_max_body_size is set appropriately to use $request_body_file effectively; otherwise, large requests will be rejected.

Be cautious about file system permissions for the temporary directory to allow NGINX to write request bodies. If not set correctly, it could lead to errors or inaccessible files.

Not all modules will handle the temporary file correctly; double-check compatibility when combining with various NGINX modules.