谈到中文简体与繁体字互转,以及汉字转拼音,大家的第一反应就是使用程序来实现,比如php,java。最近一直在nginx第三方模块上晃荡,发现nginx可以实现简繁互转并且也同时实现了转拼音的功能,特意装上简单的测试一下。
备注:测试之前告知大家目前它只支持utf8编码.
1. 安装nginx模块
1
2
|
NDK地址:
http
:
//github.com/simpl-it/ngx_devel_kit
cconv地址:
http
:
//cconv.googlecode.com/files/cconv-0.6.2.tar.gz
|
1.1 安装cconv
cconv的lib提供给nginx模块调用,实现繁体互转以及汉字转拼音的功能.
1
2
3
4
5
6
7
|
# cd /usr/local/src/
# wget http://cconv.googlecode.com/files/cconv-0.6.2.tar.gz
# tar -xzvf cconv-0.6.2.tar.gz
# cd cconv-0.6.2
# ./configure
# make
# make install
|
lib库默认安装到usr/local下,如果你是64系统执行如下命令
1
|
# ln -s /usr/local/lib/libcconv.so.0.0.0 /lib64/libcconv.so.0
|
32位执行
1
|
# ln -s /usr/local/lib/libcconv.so.0.0.0 /lib/libcconv.so.0
|
1.2 安装nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# cd /usr/local/src/
# wget https://github.com/simpl/ngx_devel_kit/archive/master.zip -O ngx_devel_kit-master.gzip
# wget https://github.com/liseen/set-cconv-nginx-module/archive/master.zip -O set-cconv-nginx-module-master.zip
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# unzip ngx_devel_kit-master.gzip
# unzip set-cconv-nginx-module-master.zip
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --with-ld-opt='-lcconv' \
--
with
-
http_stub_status_module
--
add
-
module
=
.
.
/
ngx_devel_kit
-
master
\
--
add
-
module
=
.
.
/
set
-
cconv
-
nginx
-
module
-
master
# make -j2
# make install
|
2. 指令(Directives)
set_cconv_to_simp # 繁体转简体
set_cconv_to_trad # 简体转繁体
set_pinyin_to_normal # 汉字转拼音
3. nginx配置
3.1 配置location
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
server
{
listen
80
;
server
_name
test
.
ttlsa
.
com
;
location
/
ttlsa2jianti
{
set
$
ttlsa
"運維生存時間 - www.ttlsa.com"
;
set_cconv_to
_simp
$
ttlsa
$
ttlsa
;
echo
$
ttlsa
;
}
location
/
ttlsa2fanti
{
set
$
ttlsa
"运维生存时间 - www.ttlsa.com"
;
set_cconv_to
_trad
$
ttlsa
$
ttlsa
;
echo
$
ttlsa
;
}
location
/
ttlsa2pinyin
{
set
$
ttlsa
"运维生存时间 - www.ttlsa.com"
;
set_pinyin_to
_normal
$
ttlsa
$
ttlsa
;
echo
$
ttlsa
;
}
}
|
3.2 访问测试
1
2
3
4
5
6
7
8
|
# curl http://test.ttlsa.com/ttlsa2jianti
运维生存时间
-
www
.
ttlsa
.
com
# curl http://test.ttlsa.com/ttlsa2fanti
運維生存時間
-
www
.
ttlsa
.
com
# curl http://test.ttlsa.com/ttlsa2pinyin
yunweishengcunshijian
-
www
.
ttlsa
.
com
|
繁体与简体都相互转化了,也可以转成拼音,而且全角的字母也转成了半角.
4. 注意事项
和程序一样,自定义变量不要使用程序内置的变量以及 $arg_XXX or $http_XXX。如下
1
|
set_cconv_to
_simp
$
arg
_user
'foo'
;
|
这种方法不要使用,会出问题.
5. 兼容性
以下版本通过测试
* 0.8.x (last tested version is 0.8.38)
* 0.7.x >= 0.7.46 (last tested version is 0.7.65)
* 0.5,0.6这些老版本不兼容
* 1.4.2 没问题,目前我使用的是这个版本.
转载请注明来至运维生存时间:http://www.ttlsa.com/html/3281.html
收 藏