proxy_set_body
Sets the request body to a specified value for proxying purposes. — NGINX HTTP Core
Описание
The `proxy_set_body` directive is used to alter the request body being passed to a proxied server. Usually, when NGINX proxies a request, it forwards the client's request body as received. However, there are scenarios where you may need to change this body, for example, when implementing a reverse proxy that needs to transform request data before sending it downstream. You can specify the body content as an argument to this directive, which can include arbitrary text or data that will replace the original request body. The directive can be placed in various contexts, including `http`, `server`, and `location`, allowing for flexibility in how requests are modified at different levels. When a new body is set using this directive, it overrides the original request body, and it will be sent to the proxied server as the request payload. Keep in mind that you should ensure the new body content is compatible with the request type. For instance, if the upstream server is expecting a JSON payload, the body set via `proxy_set_body` should be a valid JSON string to avoid errors or unintended behavior.
Пример конфига
location /api {
proxy_pass http://backend;
proxy_set_body '{"key":"value"}';
}Setting an empty body may lead to the proxied server receiving a blank request body.
Ensure the body format matches what the upstream server expects, or it may reject the request.