Nginx后端节点健康监测 - Wangxiaoman/tech-note GitHub Wiki

下载

Nginx下载 https://nginx.org/en/download.html

upstream_check模块 https://github.com/yaoweibin/nginx_upstream_check_module

安装

安装gcc
yum -y install make gcc gcc-c++ ncurses-devel
安装pcre
yum install -y pcre-deve
安装zlib
yum install -y zlib-devel
安装openssl
yum -y install openssl openssl-devel
打patch
patch -p0 < /xxx/nginx_upstream_check_module-master/check_1.9.2+.patch (绝对地址)
注意,如果是nginx新版本(1.12.1+),那么patch的时候,需要修改一些module的绝对路径
(/data/dev/nginx-1.13.9/src/http/modules/ngx_http_upstream_least_conn_module.c)
(/data/dev/nginx-1.13.9/src/http/ngx_http_upstream_round_robin.h)
编译nginx(注意用户、组、目录)
./configure --user=linkactive_n01 --group=linkactive_n01 --prefix=/data1/dev/nginx/nginx-1.9.9 --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --add-module=../nginx_upstream_check_module/
make
sudo make install
cp ../nginx/sbin/nginx /usr/sbin

配置

前面configure的时候指定了conf-path为/etc/nginx,所以 copy nginx编译路径下的conf文件到/etc/nginx下。 nginx.conf中的服务健康监测,参照https://github.com/yaoweibin/nginx_upstream_check_module的说明来配置

启动

sudo nginx -t 检查无误
sudo nginx 启动
sudo nginx -s reload 平滑启动
sudo nginx -s stop 停止

Nginx的日志分割

shell脚本如下(contab定时调用来分割日志文件)

BACKUP_PATH="/data1/logs/nginx"
mv  $BACKUP_PATH/access.log  $BACKUP_PATH/access_$(date -d "yesterday" "+%Y%m%d").log
mv  $BACKUP_PATH/proxy_access.log  $BACKUP_PATH/proxy_access_$(date -d "yesterday" "+%Y%m%d").log
#通知nginx将文件写到新的文件中
kill -USR1  `cat /data1/dev/nginx/nginx-1.9.9/logs/nginx.pid`