Nginx与Tengine安装和使用以及配置健康节点检测

一.Nginx与Tengine的区别:
Tengine在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验,继承Nginx-1.8.1的所有特性,兼容Nginx的配置。

二.二者安装前的准备:
2.1安装编译工具及库文件:

  1. yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2.2 安装 PCRE:
PCRE 作用是让 Nginx 支持 Rewrite 功能。
下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
在线下载:

  1. cd /usr/local/src
  2. wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

解压安装包:

  1. tar zxvf pcre-8.35.tar.gz

进入安装包目录

  1. cd pcre-8.35

编译安装

  1. ./configure[root@bogon pcre-8.35]# make && make install

查看pcre版本

  1. pcre-config --version

三.Tengine安装:
由于我们要进行的是TCP的负载均衡,所以我们要下载TCP模块,如果只对于HTTP的不用下载。下载的软件等都是放在/usr/local/src/目录中

3.1下载Tengine:
在线下载:

  1. cd /usr/local/src
  2. wget http://tengine.taobao.org/download/tengine-2.1.0.tar.gz

解压Tengine:

  1. tar -zxvf tengine-2.1.0.tar.gz

3.2 下载TCP模块:
在线下载:

  1. cd /usr/local/src
  2. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip

解压:

  1. unzip master.zip

安装path:

  1. unzip master.zip

3.3 开始安装:
进入tengine目录:

  1. cd tengine-2.1.1

打以下命令:

  1. patch -p1 < /usr/local/src/nginx_tcp_proxy_module-master/tcp.patch

出现以下信息:
patching file src/core/ngx_log.c
Hunk #1 succeeded at 69 (offset 3 lines).
patching file src/core/ngx_log.h
Hunk #1 succeeded at 30 (offset 1 line).
Hunk #2 succeeded at 38 (offset 1 line).
patching file src/event/ngx_event_connect.h
Hunk #1 succeeded at 33 (offset 1 line).
Hunk #2 succeeded at 45 (offset 2 lines).
接着执行命令:

  1. ./configure --prefix=/opt/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module
  2. --add-module=/usr/local/src/nginx_tcp_proxy_module-master

说明:—prefix=/opt/local/nginx是安装的路径 —add-module是添加TCP模块
 最后编译和安装

  1. make && make install

3.4 TCP模块和心跳检测的配置:

  1. cd /usr/local/nginx/conf
  2. vi nginx.conf

添加TCP负载均衡:

  1. tcp{
  2. upstream my{
  3. server 192.168.18.32:9000; #服务器
  4. server 192.168.18.118:60001;
  5. server 192.168.18.118:60002;
  6. server 192.168.18.118:60003;
  7. check interval=30000 fall=1 rise=2 timeout=10000 type=tcp;//心跳检测
  8. }
  9. server{
  10. listen 8080 ;
  11. proxy_pass my;
  12. }
  13. }

如果要想着界面监控后端节点:则在http里添加如下信息:

  1. server {
  2. listen 80;#端口可以改
  3. server_name localhost;
  4. location /status { #健康检查
  5. tcp_check_status;
  6. access_log off;
  7. }

然后在访问http://IP:端口/status就可以看到如下界面:

3.5心跳检测说明:

interval:向后端发送的健康检查包的间隔。
fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
timeout: 后端健康请求的超时时间。
default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
type:健康检查包的类型,现在支持以下多种类型
tcp:简单的tcp连接,如果连接成功,就说明后端正常。
ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。
http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。
ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
说明:更多配置请看官网:https://github.com/yaoweibin/nginx_tcp_proxy_module
四.Nginx安装:
nginex的安装与tengine一般相同。
可以参考这篇文章:
http://www.runoob.com/linux/nginx-install-setup.html
1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.9.9.tar.gz
[root@bogon src]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
2、解压安装包

  1. [root@bogon src]# tar zxvf nginx-1.9.9.tar.gz

3、进入安装包目录

  1. [root@bogon src]# cd nginx-1.9.9

4、编译安装

  1. [root@bogon nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35 --with-stream
  2. [root@bogon nginx-1.9.9]# make
  3. [root@bogon nginx-1.9.9]# make install

5、查看nginx版本

  1. [root@bogon nginx-1.9.9]# /usr/local/nginx/sbin/nginx -v

说明:如果要负载均衡TCP的话 记得在配置时添加—with-stream

五.补丁:Nginx节点的健康检查:
这个是淘宝技术团队开发的 nginx 模块 nginx_upstream_check_module,通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则所以的请求就不会转发到该节点上。
在淘宝自己的 tengine 上是自带了该模块的,大家可以访问淘宝tengine的官网来获取该版本的nginx,官方地址:http://tengine.taobao.org/。
如果我们没有使用淘宝的 tengine 的话,可以通过补丁的方式来添加该模块到我们自己的 nginx 中。我们业务线上就是采用该方式进行添加的。
5.1下载nginx_upstream_check_module模块

进入目录

  1. cd /usr/local/src

下载

  1. wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

解压

  1. unzip master

5.2为nginx打补丁(跟tengine打TCP补丁一样)
进入nginx的源码目录

  1. cd nginx-1.9.9

执行

  1. patch -p1 < /usr/local/src/nginx_upstream_check_module-master/check_1.5.12+.patch

配置

  1. ./configure --prefix=/usr/local/nginx
  2. --with-http_ssl_module
  3. --with-stream
  4. --with-openssl=/usr/local/src/openssl-0.9.8q
  5. --with-pcre=/usr/local/src/pcre-8.35 --add-module=usr/local/src/nginx_upstream_check_module-master/

编译
make (注意:此处只make,编译参数需要和之前的一样)

  1. mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.9.9.bak
  2. cp ./objs/nginx /usr/local/nginx/sbin/
  3. /usr/local/nginx/sbin/nginx -t # 检查下是否有问题
  4. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

5.3在nginx.conf配置文件里面的upstream加入健康检查

  1. upstream name {
  2. server 192.168.0.21:80;
  3. server 192.168.0.22:80;
  4. check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  5. }

如果要想着界面监控后端节点:则在http里添加如下信息:

  1. server {
  2. listen 80;#端口可以改
  3. server_name localhost;
  4. location /status { #健康检查
  5. check_status;
  6. access_log off;
  7. }

然后在访问http://IP:端口/statu

上面配置的意思是,对name这个负载均衡条目中的所有节点,每个3秒检测一次,请求2次正常则标记 realserver状态为up,如果检测 5 次都失败,则标记 realserver的状态为down,超时时间为1秒。

六.关于nginx的其他命令:

启动
/usr/local/nginx/sbin/nginx
重新载入配置文件
/usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
重启
/usr/local/nginx/sbin/nginx -s reopen # 重启 Nginx
停止
/usr/local/nginx/sbin/nginx -s stop # 停止 Nginx
查看tcp端口状态
netstat -apn|grep :端口