How to set charset of all text responses on nginx
All text files on a site usually share the same character encoding.
Especially UTF-8 is the modern de facto standard. However, the default
charset_types
does not contain text/css
, let alone other non-plain
text types like text/markdown
.
The default charset_types
should be text/*
because most of them are parsed
in ASCII (us-ascii
) by default for backward compatibility. A
text/xml
response is parsed in ASCII even if BOM and XML
declaration tells otherwise. Therefore, we should use
application/xml
for XML responses now.
Nevertheless, the charset_types
setting checks complete matches only.
Luckily, the map
directive knows regex, and charset_types
accepts a
variable.
map $sent_http_content_type $charset {
~^text/ utf-8;
}
charset $charset;
charset_types *;
This setting would make nginx specify UTF-8 for all text responses, such as
text/css; charset=utf-8
.