charset_types
The `charset_types` directive defines the MIME types for which the specified character set is set in NGINX.
Description
The charset_types directive in NGINX allows administrators to specify which content types will have a certain character encoding applied. This directive accepts one or more MIME types as arguments and must be used within an http, server, or location block. When a response matches one of the defined MIME types, NGINX automatically adds the appropriate Content-Type header for that character set, thereby informing clients how to properly interpret the response data.
For instance, if you define charset_types text/html application/json;, NGINX will send the character set defined by the charset directive for any response with Content-Type: text/html or Content-Type: application/json. This is particularly useful for ensuring proper text rendering on clients by defining a default character set for different file types, enhancing compatibility with multibyte character encodings such as UTF-8.
If a request's content type does not match any of the types specified in charset_types, NGINX will not apply the default character set defined by the charset directive for those responses, allowing for fine-grained control over how different content types handle character encoding.
Config Example
http {
charset UTF-8;
charset_types text/html application/json text/css;
}
If charset is not set in your configuration, then charset_types will have no effect as there is no character set to apply to the types specified.
Be cautious when setting charset_types as it affects all matching responses across the defined context, which may lead to unexpected behavior if not controlled properly.