set_escape_uri

The `set_escape_uri` directive encodes a specified value into a URI-safe string by escaping certain characters.

Syntaxset_escape_uri variable_name [input_value];
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments1-2

Description

The set_escape_uri directive in the NGINX Set-Misc module is used to transform a string variable into a URI-escaped format. This is particularly useful when the original value contains characters that are not safe to include directly in a URI. The directive takes either one or two arguments. The first argument is the name of the variable to set, and the second (optional) argument specifies the input data to be escaped. If the second argument is not provided, the first argument is assumed to be the source variable with the value to escape.

When invoked, this directive will analyze the input string and replace unsafe characters with their percent-encoded equivalents. For example, spaces will become %20, and special symbols will be escaped according to the URI standards. This is essential for ensuring URLs are valid and can be correctly processed by web servers and browsers. The directive can be used in various contexts, including http, server, location, and conditional blocks, thus offering flexibility in configuration.

Config Example

location /example {
    set $user_input $arg_input;
    set_escape_uri $escaped_input $user_input;
}

Ensure the variable being escaped is properly set before using this directive.

If the input variable contains null bytes, the behavior may be unpredictable.

Using set_escape_uri inside an if block may lead to unexpected results if combined with other directives that change the request flow.

← Back to all directives