ngx_http_charset_module 模組

範例配置
指令
     charset
     charset_map
     charset_types
     override_charset
     source_charset

ngx_http_charset_module 模組會在 “Content-Type” 回應標頭欄位中加入指定的字元集。此外,此模組可以將資料從一個字元集轉換為另一個字元集,但有一些限制:

範例配置

include        conf/koi-win;

charset        windows-1251;
source_charset koi8-r;

指令

語法 charset charset | off;
預設
charset off;
Context http, server, location, if in location

在 “Content-Type” 回應標頭欄位中加入指定的字元集。如果此字元集與 source_charset 指令中指定的字元集不同,則會執行轉換。

參數 off 會取消在 “Content-Type” 回應標頭欄位中加入字元集。

可以使用變數定義字元集。

charset $charset;

在這種情況下,變數的所有可能值都必須在配置中以 charset_mapcharsetsource_charset 指令的形式至少出現一次。對於 utf-8windows-1251koi8-r 字元集,只需將 conf/koi-winconf/koi-utfconf/win-utf 檔案包含到配置中即可。對於其他字元集,簡單地建立一個虛構的轉換表即可,例如:

charset_map iso-8859-5 _ { }

此外,字元集可以設定在 “X-Accel-Charset” 回應標頭欄位中。可以使用 proxy_ignore_headersfastcgi_ignore_headersuwsgi_ignore_headersscgi_ignore_headersgrpc_ignore_headers 指令來停用此功能。

語法 charset_map charset1 charset2 { ... }
預設
Context http

描述從一個字元集到另一個字元集的轉換表。反向轉換表使用相同的資料建立。字元代碼以十六進位表示。範圍 80-FF 中遺失的字元會被替換為 “?”。從 UTF-8 轉換時,單一位元組字元集中遺失的字元會被替換為 “&#XXXX;”。

範例

charset_map koi8-r windows-1251 {
    C0 FE ; # small yu
    C1 E0 ; # small a
    C2 E1 ; # small b
    C3 F6 ; # small ts
    ...
}

在描述到 UTF-8 的轉換表時,UTF-8 字元集的代碼應該在第二欄中給出,例如:

charset_map koi8-r utf-8 {
    C0 D18E ; # small yu
    C1 D0B0 ; # small a
    C2 D0B1 ; # small b
    C3 D186 ; # small ts
    ...
}

在發行檔案 conf/koi-winconf/koi-utfconf/win-utf 中提供了從 koi8-rwindows-1251,以及從 koi8-rwindows-1251utf-8 的完整轉換表。

語法 charset_types mime-type ...;
預設
charset_types text/html text/xml text/plain text/vnd.wap.wml
application/javascript application/rss+xml;
Context httpserverlocation

此指令在 0.7.9 版中出現。

除了 “text/html” 之外,還會在具有指定 MIME 類型的回應中啟用模組處理。特殊值 “*” 會比對任何 MIME 類型 (0.8.29)。

在 1.5.4 版之前,預設的 MIME 類型是使用 “application/x-javascript” 而不是 “application/javascript”。

語法 override_charset on | off;
預設
override_charset off;
Context http, server, location, if in location

決定當從代理或 FastCGI/uwsgi/SCGI/gRPC 伺服器收到的回應的 “Content-Type” 回應標頭欄位中已帶有字元集時,是否應執行轉換。如果啟用轉換,則接收到的回應中指定的字元集會被用作來源字元集。

應注意,如果在子請求中收到回應,則無論 override_charset 指令設定為何,都會始終執行從回應字元集到主要請求字元集的轉換。

語法 source_charset charset;
預設
Context http, server, location, if in location

定義回應的來源字元集。如果此字元集與 charset 指令中指定的字元集不同,則會執行轉換。