echo

The `echo` directive outputs a specified string or data in the response body.

Syntaxecho string;
Defaultnone
Contextlocation, if in location
Argumentsany

Description

The echo directive allows NGINX to return strings or data directly to the client in response to HTTP requests. It can be used in various contexts such as location or inside an if statement within a location. This directive is highly flexible, accepting any string as an argument to be returned to the client. It can also handle special formatting, including variables and expressions, making it a powerful tool for outputting custom messages or information from within the NGINX configuration.

When executed, the echo directive sends the specified string as part of the HTTP response body. The directive can be combined with other echo-related commands, such as echo_sleep, echo_flush, and echo_reset_timer, allowing for more complex interactivity and control over the output. For instance, echo_sleep can introduce delays, and echo_flush can send partial responses at different stages of processing.

Additionally, since echo is context-sensitive, it can be placed within conditionals to dynamically determine what gets output based on certain request parameters or headers. This makes echo not only a straightforward output mechanism but also a part of a more dynamic response system.

Config Example

location /greet {
    echo "Hello, welcome to NGINX!";
}

Ensure that the string is properly quoted to avoid syntax errors.

Be careful not to use echo in a manner that conflicts with other output-handling directives like return or rewrite.

The output and its processing may not be compatible with certain settings, such as buffering or client timeout configurations.

← Back to all directives