模块 ngx_http_charset_module


示例Directives
charset
charset_map
charset_types
override_charset
source_charset

该模块将指定的 charset 添加到 “Content-Type” 响应标头字段中。 此外,该模块可以将数据从一个字符集转换为另一个字符集, 有一些限制:ngx_http_charset_module

  • 转换以一种方式执行 — 从服务器到客户端,
  • 只能转换单字节字符集
  • 或单字节字符集与 UTF-8 之间的转换。

示例配置

include        conf/koi-win;

charset        windows-1251;
source_charset koi8-r;

Directives

语法: charset charset | off;
默认值:
charset off;
上下文: http, , ,serverlocationif in location

将指定的字符集添加到 “Content-Type” response header 字段。 如果此字符集与指定的字符集不同 在 source_charset Directives中,执行转换。

该参数取消添加 charset 添加到 “Content-Type” 响应标头字段。off

可以使用变量定义字符集:

charset $charset;

在这种情况下,需要存在变量的所有可能值 在配置中至少以 charset_mapcharsetsource_charset Directives的形式进行一次。 对于 、 和 字符集,将文件 、 和 包含在配置中就足够了。 对于其他字符集,只需制作一个虚构的转换表即可, 例如:utf-8windows-1251koi8-rconf/koi-winconf/koi-utfconf/win-utf

charset_map iso-8859-5 _ { }

此外,可以在 “X-Accel-Charset” 响应标头字段。 可以使用 proxy_ignore_headersfastcgi_ignore_headersuwsgi_ignore_headers、scgi_ignore_headers 禁用此功能。 和 grpc_ignore_headers Directives。

语法: charset_map charset1 charset2 { ... }
默认值:
上下文: 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
    ...
}

分发文件 、 、 和 中提供了从 到 、 从 和 到 的完整转换表 。koi8-rwindows-1251koi8-rwindows-1251utf-8conf/koi-winconf/koi-utfconf/win-utf

语法: charset_types mime-type ...;
默认值:
charset_types text/html text/xml text/plain text/vnd.wap.wml
application/javascript application/rss+xml;
上下文: http, ,serverlocation

该Directives出现在 0.7.9 版本中。

在具有指定 MIME 类型的响应中启用模块处理 除了 “”之外。 特殊值 “” 与任何 MIME 类型 (0.8.29) 匹配。text/html*

在版本 1.5.4 之前,使用的是 “” 作为默认 MIME 类型,而不是 “”。application/x-javascriptapplication/javascript

语法: override_charset on | off;
默认值:
override_charset off;
上下文: http, , ,serverlocationif in location

确定是否应对答案执行转换 从代理或FastCGI/uwsgi/SCGI/gRPC服务器接收 当答案已经在 “Content-Type” 中带有 charset 时 response header 字段。 如果启用了转换,则会收到 response 用作源字符集。

应该注意的是,如果在子请求中收到响应 然后从响应 charset 到主请求 charset 的转换 始终执行,而不管Directives设置如何。override_charset

语法: source_charset charset;
默认值:
上下文: http, , ,serverlocationif in location

定义响应的源字符集。 如果此字符集与指定的字符集不同 在 charset Directives中,执行转换。