iconv_filter

The iconv_filter directive in NGINX enables character encoding conversion for response bodies before they are sent to the client.

Syntaxiconv_filter from= to=;
Defaultnone
Contextlocation
Arguments2

Description

The iconv_filter directive is part of the NGINX iconv module which utilizes the libiconv library to convert character encodings in response body data. This directive is specified within a location block and takes two parameters: the source encoding (from) and the target encoding (to). When this directive is applied, it modifies the output content generated by that location, converting it from the specified source encoding to the target encoding. This is particularly useful when serving content that needs to be in a specific encoding to accommodate client requirements or preferences.

The directive works by hooking into NGINX's output filtering mechanism, allowing it to process the body of the HTTP response. The conversions are handled in chunks, with the directive controlling how data is read, converted, and then output. The iconv_buffer_size directive is often used in conjunction with iconv_filter to define the size of the buffer used during the conversion process. If not specified, NGINX will use a system default buffer size. Additionally, the set_iconv directive can be utilized to perform encoding transformations on individual variables within the NGINX configuration, offering flexibility for dynamic content generation.

Config Example

location /example {
    iconv_filter from=utf-8 to=gbk;
    iconv_buffer_size 1k;
    # other configurations
}

Ensure that the iconv module is correctly installed and loaded in NGINX; it is not included by default.

Using incorrect character encoding names can lead to conversion failures; validate the specified encodings.

Remember that content type should be compatible with the target encoding to avoid displaying garbled text.

← Back to all directives