NoCache

Table of Contents

Fix "Â" Showing Up in Static Files (Set Charset to UTF-8 in Nginx)

Cyrus Kao
Last modified on .

I recently had a very weird issue with Nginx serving static content. A Latin-script letter  keeps showing up in one of my CSS files, and the letters are not in the actual file. It turns out that Nginx doesn't set chartset by default, that's why the characters not in the ISO basic alphabet got decoded to something else.

Set Charset

We'll be using ngx_http_charset_module to configure charset in Nginx. This module comes by default, no extra installation needed.

Enable Charset

charset is off by default, set it to utf-8 so it appends charset=utf-8 after MIME type in content-type header:

charset utf-8;
Nginx config

charset can be set in http, server, location blocks.

Select Types to Append Charset

By default, Nginx will only append charset to MIME types of text/html, text/xml, text/plain, text/vnd.wap.wml, application/javascript, application/rss+xml. To change that:

charset_types text/css text/xml text/plain text/vnd.wap.wml application/javascript application/rss+xml;
Nginx config

charset_types can be set in http, server, location blocks.

text/html can be excluded, since it's enabled by default according to the official documentation.

Reload Nginx

Reload Nginx to apply changes:

$ sudo nginx -s reload

Verify Charset in Headers

Use development tools of your browser to verify static files are served with charset=utf-8 in content-type:

DevTools
DevTools in Chromium

Comments

Sign in to leave a comment.