$request_body
The $request_body variable contains the raw body of the client request. — NGINX Core (HTTP)
Description
The $request_body variable in NGINX captures the body of the client request after it has been processed by the server. It is primarily populated when the request method is one that typically contains a body, such as POST or PUT. This variable is only available if the 'set' or 'get' request body processing is enabled in the server configuration, usually through the proxy_pass directive or if the client sends data directly to the server. Once set, it holds all data sent by the client, making it useful for processing forms, file uploads, or any payloads sent in requests. When utilizing the $request_body variable, it is crucial to note that the request body is buffered. By default, it might be limited by the client_max_body_size directive, which restricts the size of the body that is processed and ultimately stored in this variable. If the body exceeds this limit, the request may be rejected, and the variable won’t hold any data. Typical values for $request_body can include JSON payloads, multipart form data, or simple text, depending on the content type and the client's intent.
Config Example
server {
listen 80;
server_name example.com;
location /submit {
# Capture the request body
proxy_pass http://backend;
proxy_set_header X-Request-Body $request_body;
}
}Subsystem
httpCacheable
YesContexts
http, server, location, ifEnsure client_max_body_size is set appropriately to avoid issues with larger request bodies being discarded.
Remember that $request_body is only available for methods that support a body, such as POST and PUT.
If using post-processing in a subsequent location block, make sure previous location blocks do not consume the request body if you intend to access it later.