set_decode_hex

The `set_decode_hex` directive decodes a hexadecimal string into its binary representation.

Syntaxset_decode_hex var name [hex_string];
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments1-2

Description

The set_decode_hex directive is part of the NGINX Set-Misc module, which provides various utilities for string manipulation within the NGINX configuration. This directive specifically decodes a hexadecimal string provided as an argument into its binary form. By using this directive, you can effectively convert a hex-encoded string back into its original binary data for further processing or serving.

This directive takes 1-2 arguments, where the first argument indicates the variable that will hold the output binary data, and the second (optional) argument is the hex string to be decoded. If the second argument is omitted, NGINX defaults to decoding the input directly from the request, such as query parameters or headers. The directive operates in contexts that include http, server, location, and within if blocks, allowing flexible use throughout the configuration.

It's important to note that the input string must be a valid hexadecimal representation; otherwise, the directive might not produce valid binary output. Additionally, if the provided hex string has an odd length, the decoder may handle it gracefully but can lead to unexpected results, so ensuring even length hex inputs is advisable.

Config Example

location /decode {
    set $output "";
    set_decode_hex $output $arg_hex;
    # The variable $output now contains the decoded binary data from the hex input.
}

Using an odd-length hexadecimal string will lead to unexpected results or errors in decoding.

Make sure the variable used for output is defined before using this directive.

← Back to all directives