模块 ngx_http_charset_module
示例Directives charset charset_map charset_types override_charset source_charset |
这ngx_http_charset_module
module 将指定的
charset 添加到 “Content-Type” 响应标头字段中。
此外,该模块可以将数据从一个字符集转换为另一个字符集,
有一些限制:
- 转换以一种方式执行 — 从服务器到客户端,
- 只能转换单字节字符集
- 或单字节字符集与 UTF-8 之间的转换。
示例配置
include conf/koi-win; charset windows-1251; source_charset koi8-r;
Directives
语法: |
charset |
---|---|
默认值: |
charset off; |
上下文: |
http ,server ,location ,if in location |
将指定的字符集添加到 “Content-Type” response header 字段。 如果此字符集与指定的字符集不同 在 source_charset Directives中,执行转换。
参数off
取消添加 charset
添加到 “Content-Type” 响应标头字段。
可以使用变量定义字符集:
charset $charset;
在这种情况下,需要存在变量的所有可能值
在配置中至少以 charset_map、charset 或 source_charset Directives的形式进行一次。
为utf-8
,windows-1251
和koi8-r
charset 的 cookie 设置,则包含文件conf/koi-win
,conf/koi-utf
和conf/win-utf
进入配置。
对于其他字符集,只需制作一个虚构的转换表即可,
例如:
charset_map iso-8859-5 _ { }
此外,可以在 “X-Accel-Charset” 响应标头字段。 可以使用 proxy_ignore_headers、fastcgi_ignore_headers、uwsgi_ignore_headers、scgi_ignore_headers 禁用此功能。 和 grpc_ignore_headers Directives。
语法: |
charset_map |
---|---|
默认值: | — |
上下文: |
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-r
自windows-1251
和 fromkoi8-r
和windows-1251
自utf-8
在分发文件中提供conf/koi-win
,conf/koi-utf
和conf/win-utf
.
语法: |
charset_types |
---|---|
默认值: |
charset_types text/html text/xml text/plain text/vnd.wap.wml application/javascript application/rss+xml; |
上下文: |
http ,server ,location |
该Directives出现在 0.7.9 版本中。
在具有指定 MIME 类型的响应中启用模块处理
除了”text/html
”.
特殊值 “” 与任何 MIME 类型 (0.8.29) 匹配。*
在 1.5.4 版本之前,”application/x-javascript
“ 被使用 作为默认 MIME 类型,而不是 ”application/javascript
”.
语法: |
override_charset |
---|---|
默认值: |
override_charset off; |
上下文: |
http ,server ,location ,if in location |
确定是否应对答案执行转换 从代理或FastCGI/uwsgi/SCGI/gRPC服务器接收 当答案已经在 “Content-Type” 中带有 charset 时 response header 字段。 如果启用了转换,则会收到 response 用作源字符集。
应该注意的是,如果在子请求中收到响应
然后从响应 charset 到主请求 charset 的转换
始终执行,而不管override_charset
Directives设置。
语法: |
source_charset |
---|---|
默认值: | — |
上下文: |
http ,server ,location ,if in location |
定义响应的源字符集。 如果此字符集与指定的字符集不同 在 charset Directives中,执行转换。