1. 程式人生 > 實用技巧 >NGINX 增加charset=utf-8

NGINX 增加charset=utf-8

由於部分老裝置頁面編碼不是utf-8 ,導致頁面亂碼。 通過配置nginx的charset 對裝置進行相容。

期間發現單純charset utf-8 的配置並不能使請求生效。與網上看到的攻略存在差異,特此輸出本文。

最後生效的配置:

  • charset_types * ;# 由於1.5.4版本開始預設MIME type 為“application/x-javascript” ,通過 * 號覆蓋text/css 、application/javascript
  • charset utf-8 ; # 預設為 off

官方引數說明:

Syntax:	charset charset | off;
Default:	charset off;
Context:	http, server, location, if in location

  

Adds the specified charset to the “Content-Type” response header field. If this charset is different from the charset specified in thesource_charsetdirective, a conversion is performed.

The parameteroffcancels the addition of charset to the “Content-Type” response header field.

A charset can be defined with a variable:

charset $charset;

In such a case, all possible values of a variable need to be present in the configuration at least once in the form of thecharset_map,charset, orsource_charsetdirectives. Forutf-8,windows-1251, andkoi8-rcharsets, it is sufficient to include the filesconf/koi-win,conf/koi-utf, andconf/win-utf

into configuration. For other charsets, simply making a fictitious conversion table works, for example:

charset_map iso-8859-5 _ { }

In addition, a charset can be set in the “X-Accel-Charset” response header field. This capability can be disabled using theproxy_ignore_headers,fastcgi_ignore_headers,uwsgi_ignore_headers,scgi_ignore_headers, andgrpc_ignore_headersdirectives.

Syntax:	charset_types mime-type ...;
Default:	charset_types text/html text/xml text/plain text/vnd.wap.wml
                application/javascript application/rss+xml;
Context:	http, server, location
This directive appeared in version 0.7.9.

  

Enables module processing in responses with the specified MIME types in addition to “text/html”. The special value “*” matches any MIME type (0.8.29).

Until version 1.5.4, “application/x-javascript” was used as the default MIME type instead of “application/javascript”.