这个利用nginx(openresty)+lua+opencc,写10行代码左右就可以实现的类似繁简通的功能 可充分利用nginx的快反应和可靠性
所需插件
https://github.com/BYVoid/OpenCC
https://github.com/Finalcheat/lua-resty-opencc/blob/master/lib/resty/opencc.lua
opencc在centos7可以用yum安装,但版本很旧 yum install doxygen opencc opencc-tools opencc-devel centos7上编译新版本可能要升级gcc,比较麻烦,我在centos8上编译过新版本 参考: yum install cmake gcc-c++ bison flex http://www.doxygen.nl/download.html https://bintray.com/byvoid/opencc/OpenCC ln -s /user/bin/python3.6 /user/bin/python ln -s /usr/lib/libopencc.so.2 /usr/lib64/libopencc.so.2
安装openresty后opencc.lua放到/usr/local/openresty/lualib/resty/
opencc.lua
if (ngx.var.sent_http_content_type ~= nil and string.sub(ngx.var.sent_http_content_type, 1, 4) == "text") then
local data=ngx.ctx.data or ""
local cdata=ngx.arg[1]
data=data..cdata
ngx.ctx.data=data
if ngx.arg[2] then
local opencc = require("resty.opencc")
-- local o = opencc:new("zhs2zht.ini") -- opencc 0.4.x
local o = opencc:new("s2hk.json")
ngx.log(ngx.ERR, " ngx.arg[1]:", data)
local c = o:convert(data)
o:close()
ngx.arg[1] = c
else
ngx.arg[1] = ""
end
end
nginx_file.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name localhost;
location / {
proxy_pass https://www.eweb.hk;
body_filter_by_lua_file lua/opencc.lua;
}
}
}