echo_before_body

The `echo_before_body` directive sends a response body before the main body of the HTTP response is sent.

Syntaxecho_before_body string;
Defaultnone
Contextlocation, if in location
Argumentsany

Description

The echo_before_body directive is a part of the NGINX Echo module that allows users to send arbitrary content as part of the response prior to transmitting the main response body. This can be particularly useful for scenarios such as injecting headers or modifying the output stream based on certain conditions or computations before the main content is processed and sent to the client. The directive can be used in a location block or within an if block comprised in a location context, enabling tailored response outputs depending on specific requests.

When used, the directive does not define a fixed value but rather accepts any argument. This argument can be a simple string or a more complex expression to produce dynamic content. Once the echo_before_body directive is triggered, it writes to the response output stream, and the NGINX web server will handle the response stream accordingly while preparing to send the full response to the client. Therefore, it's essential that users are aware of the order and rules of execution in their configuration, as sending data out of order (specifically before the main body) can impact the expected behavior of the HTTP response.

It's important to note that this directive does not terminate the request. Instead, it prepares the output stream to include the specified content along with any subsequent processing that might occur. Users should be careful with how much data they echo before the body, as excessive use can lead to larger response sizes than anticipated, potentially affecting bandwidth and performance.

Config Example

location /example {
    echo_before_body "Initial Content";
    echo "Main Response Body";
}

Using echo_before_body incorrectly can lead to unexpected response formats.

Defining too large content may affect performance and bandwidth.

Order of directives is crucial; ensure echo_before_body is used in the correct sequence relative to other directives.

← Back to all directives