Tengine简介和安装使用方法
Tengine是淘宝开发的基于nginx的应用服务器,反向代理服务器。它兼容nginx的所有配置,并且在其上增加了很多实用的功能,例如动态模块加载,集成lua语言进行扩展等,在性能方面有了比较大的改进。下面是Tengine独有的部分功能,查看全部等前往官网,地址是 http://tengine.taobao.org/。
一、Tengine部分特性
1.nginx -m 查看所有已经加载的模块,static说明已经安装的,shared说明是动态安装的;
2.nginx架构是模块化的,除了主模块,其他模块都需在安装时进行编译安装,也可在安装后进行编译安装;
3.tengine完全兼容nginx的配置;
4.tengine实现动态加载模块,不需重新编译,就可以实现安装新的模块。动态模块功能会被默认安装。而nginx不支持模块动态加载,需要编译重启;
5.lua语言扩展功能,通过编程可以有效为应用服务器分流请求;
6.强大的负载均衡功能,可以检测应用服务器的健康情况,动态的上下线。
二、安装Tengine,具有LUA模块
1.安装PCRE库,如有可以不装
cd /usr/local/
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre2-10.10.tar.gz
tar -zxvf pcre2-10.10.tar.gz
cd pcre2-10.10
./configure
make
make install
2.安装zlib库,如有可以不装
cd /usr/local/
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure
make
make install
3.安装ssl,如有可以不装
cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
4.安装lua-jit,即时编译的lua
下载wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz,安装时需要root权限,解压缩,打开根目录,使用下列命令,安装到默认位置。
make && sudo make install
5.安装ngx_devel_kit,下载wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz,解压
6.安装ngx_lua,下载wget https://github.com/openresty/lua-nginx-module/archive/v0.9.16rc1.tar.gz,解压
7.export LUAJIT_LIB=/usr/local/lib,可以查看lua-jit安装配置文件获取lib和include的值;
8.export LUAJIT_INC=/usr/local/include/luajit-2.0 ;
9.安装tengine,下载tengine,解压缩,打开目录,执行下列命令:
./configure --prefix=/home/work/opt/nginx --with-ld-opt='-Wl,-rpath,/usr/local/lib' --add-module=/home/work/tengine/ngx_devel_kit-0.2.19 --add-module=/home/work/tengine/lua-nginx-module-0.9.16rc1 --with-openssl=/home/work/tengine/openssl-1.0.1c
make
make install
参考文档:
http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3118061.html
http://tengine.taobao.org/documentation_cn.html
https://github.com/openresty/lua-nginx-module/blob/master/README.markdown
三、使用Tengine
1.配置
提供一个典型配置,包括负载均衡,记录日志,限流,使用LUA扩展等功能。如下:
#user nobody;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 2048;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent $request_time $upstream_response_time $bytes_sent $request_length ';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 10;
#限制域limit_req_zone $binary_remote_addr $uri zone=one:30m rate=10r/s;
limit_req_zone $binary_remote_addr $uri zone=one:30m rate=10r/s;
proxy_next_upstream error http_500 http_503 http_504;
#proxy_next_upstream timeout;
proxy_connect_timeout 5s;
proxy_send_timeout 10s;
upstream onlineProxy {
server ip:port;
server ip:port;
server ip:port;
server ip:port;
server ip:port;
server ip:port;
}
upstream standaloneProxy {
server ip:port;
server ip:port;
server ip:port;
server ip:port;
server ip:port;
server ip:port;
}
server {
listen 8881;
server_name 域名;
proxy_intercept_errors on;
location ^~ /xxx/ {
#100表示最大请求数阀值,超过限制域(1r/s)请求会被延迟,超过阀值次数的请求会返回503,nodeley表示超过的限制域的请求直接失败
limit_req zone=one burst=100;
access_log logs/online/access_online.log main;
proxy_pass http://onlineProxy;
error_page 404 500 502 503 504 /error_online.html;
proxy_set_header x-forward-nginx $remote_addr;
}
location /lua_content {
# MIME type determined by default_type:
default_type 'text/plain';
access_log logs/test.log main;
content_by_lua "ngx.say('Hello,world!')";
}
location ^~ /xxx/ {
access_log logs/standalone/access_standalone.log main;
proxy_pass http://standaloneProxy;
error_page 404 500 502 503 504 /error_standalone.html;
proxy_set_header x-forward-nginx $remote_addr;
}
location ^~ /gamesdkjobs/uploadify/uploadFile/ {
access_log logs/standalone/access_standalone.log main;
proxy_pass http://10.10.1.35:8181/gamesdkjobs/uploadify/uploadFile/;
error_page 404 500 502 503 504 /error_standalone.html;
proxy_set_header x-forward-nginx $remote_addr;
}
location = /monitor {
default_type application/json;
return 200 'success';
}
location / {
return 404;
}
location = /error_online.html {
access_log logs/online/access_online_error.log;
root html;
}
location = /error_standalone.html {
access_log logs/standalone/access_standalone_error.log;
root html;
}
}
}
2.启动
opt/nginx/sbin/nginx
3.重启
编写重启脚本,如下:
pid_path="/home/work/opt/nginx/logs/nginx.pid"
kill -HUP `cat ${pid_path}`
4.停止
编写停止脚本,如下:
pid_path="/home/work/opt/nginx/logs/nginx.pid"
kill -QUIT `cat ${pid_path}`
5.分割访问日志
编写定时任务来定期执行脚本,如下:
logs_path="/home/work/opt/nginx/logs/"
pid_path="/home/work/opt/nginx/logs/nginx.pid"
log_date=`date -d "1 minute ago" +%Y%m%d%H`
mv ${logs_path}online/access_online.log ${logs_path}online/access_online.log.${log_date}
kill -USR1 `cat ${pid_path}`
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐