set_iconv
The `set_iconv` directive performs character set conversion on specified variables using iconv.
Description
The set_iconv directive in the NGINX iconv module enables the conversion of character encodings for variables within a specified context. It accepts four parameters: the destination variable, the source variable, the 'from' encoding, and the 'to' encoding. Upon execution, the directive reads the contents of the source variable, converts it from the specified 'from' encoding to the 'to' encoding, and stores the result in the destination variable. This conversion leverages the iconv library, allowing for communications between different character sets, which is particularly useful in multi-lingual applications.
The parameters are specified as set_iconv <destination_variable> <source_variable> from=<from_encoding> to=<to_encoding>. The directive operates in the location context and executes during the rewrite phase, making it suitable for dynamically altering variables based on request properties before they are processed further. The directive is versatile and poses a straightforward approach to managing character encoding in content served by NGINX, ensuring that text is properly encoded when originating from one character set but needs to be displayed in another.
Config Example
location /example {
set $src 'Hello';
set_iconv $dst $src from=UTF-8 to=ISO-8859-1;
# Usage of $dst in further processing would now show 'Hello' in ISO-8859-1 encoding.
}Ensure that the specified encodings are supported by the iconv library installed on your server.
Be cautious with output formats; improper conversion can lead to character loss or corruption.
The directive must be placed within an appropriate context—it cannot be used globally outside of a location block.