首页
首页
文章目录
  1. 简介
  2. 下载相关内容
  3. 对nginx-1.14打补丁
  4. 编译及安装
  5. 创建开机启动systemd服务
  6. 修改upstream参数
  7. 其它

nginx编译安装并增加nginx_upstream_check_module-master实现主动监测

简介

下载相关内容

下载nginx对应版本以及nginx_upstream版本,安装依赖

1
2
3
4
5
[root@nginx1 ~]# wget http://nginx.org/download/nginx-1.14.1.tar.gz
[root@nginx1 ~]# wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
[root@nginx1 ~]# yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel patch unzip
[root@nginx1 ~]# tar zxvf nginx-1.14.1.tar.gz
[root@nginx1 ~]# unzip master

对nginx-1.14打补丁

进入nginx解压目录

1
2
[root@nginx1 nginx-1.14.1]# /root/nginx/nginx-1.14.1
[root@nginx1 nginx-1.14.1]# patch -p1 < /tmp/nginx_upstream_check_module-master/check_1.14.0+.patch

编译及安装

1
2
[root@nginx1 nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_ssl_module  --lock-path=/usr/local/nginx/run/nginx.lock --pid-path=/usr/local/nginx/run/nginx.pid --add-module=/root/nginx/nginx_upstream_check_module-master
[root@nginx1 nginx-1.14.1]# make && make install

注意 如果是升级的话尽量不需要选择与当前安装目录相同的目录,以免影响正在运行的nginx

创建开机启动systemd服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@nginx1 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

修改upstream参数

1
2
3
4
5
6
upstream smart-his{
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
check interval=3000 rise=2 fall=5 timeout=2000 type=tcp; # 每隔三秒检查一次 超时时间为2秒, 失败5次配置节点为DOWN, 2次正常恢复为UP
}

1
2
3
4
5
6
7
8
9
upstream smart-his{
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
server 192.168.200.201:80 max_fails=1 fail_timeout=10s weight=100;
check interval=3000 rise=2 fall=5 timeout=2000 type=http;
check_http_send "GET /ip.html HTTP/1.1\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
# 每隔三秒使用GET方法通过HTTP1.1协议打开 http://server/ip.html如果状态码为2xx或3xx就表示服务为UP状态,超时时间为2秒,失败5次配置节点为DOWN, 2次正常恢复为UP
}

二选一根据需求来选择, TCP、或http
###check参数解释

  • upstream 后面的www不是域名.可以根据需要自己起。

  • upstream 后面的www 要和proxy_pass http://www; 里面的www相互对应.

  • 注解:check interval=3000 rise=2 fall=5 timeout=1000;

    • interval检测间隔时间,单位为毫秒。
    • rise请求2次正常的话,标记此realserver的状态为up。
    • fall表示请求5次都失败的情况下,标记此realserver的状态为down。
    • timeout为超时时间,单位为毫秒。
  • 语法

    1
    check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
  • 默认配置
    如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp

  • upstream整个参数
    Context: upstream
    该指令可以打开后端服务器的健康检查功能。
    指令后面的参数意义是:

  • 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。
  • 其他语法
    1
    2
    3
    4
    Syntax: check_keepalive_requests request_num
    Default: 1
    Context: upstream
    该指令可以配置一个连接发送的请求数,其默认值为1,表示Tengine完成1次请求后即关闭连接。
1
2
3
4
5
6
Syntax: check_http_send http_packet

Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream
该指令可以配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用"HEAD"方法。
当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常。
1
2
3
4
Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream
该指令指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。

其它

针对nginx反向代理后端的iis服务时可能会出现

1
recv() failed (104: Connection reset by peer)

这个错误日志recv() failed (104: Connection reset by peer)是在后端为IIS的情况下抛出的,抓包发现IIS并不会发fin包来断开链接,而是在超时后发RST包重置连接,所以导致了这个问题。
不影响使用,如果想要修改
请参考:
https://github.com/alibaba/tengine/issues/901
重新编译一下即可解决

以上
End!

支持一下
扫一扫,我会更有动力更新
  • 微信扫一扫
  • 支付宝扫一扫