简介: nginx_upstream_check_module是由 淘宝技术团队开发的一个 Tengine(nginx 的分支) 的模块,Tengine 自带这个模块(2.0版本开始tengine也取消了自带,需要自己编译安装), nginx的单独安装模块。

作用: 用于提供主动式后端服务器健康检查。通过它可以检测后端 realserver 的健康状态,如果后端 realserver 不可用,则所有的请求就不会转发到该节点上

心跳检测的意义:如果有一台web服务器死机,或工作出现故障,服务能立即将其检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后再自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

编译安装tengine

nginx版本选择nginx-tengine直接编译安装即可,不添加编译模块,后期再自行添加。

本文建立在编译安装的基础上。如果选择的快速安装,不适合本安装。

如果是安装的nginx, 会有所不同,下文有区别介绍

下载解压

定位到/www/server/nginx/src/

  • nginx-tengine经过宝塔编译安装,nginx_upstream_check_module模块已经放在/www/server/nginx/src/modules/ngx_http_upstream_check_module
  • nginx则需要下载,安装下方命令下载。
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
upzip master

定位安装目录

cd /www/server/nginx/src/

查看当前的编译命令

nginx -V

Nginx-tengine 在当前编译命令后加上 --add-module=/www/server/nginx/src/modules/ngx_http_upstream_check_module

Nginx 则是--add-module=/www/server/nginx/src/nginx_upstream_check_module注意替换下方命令

执行编译

./configure #后面接编译命令加上nginx_upstream_check_module
#例子
./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/www/server/nginx/src/modules/ngx_http_upstream_check_module
make && make install

生成的新nginx命令在 objs目录下

替换之前的nginx命令

cp objs/nginx /usr/bin/nginx

重启nginx

service nginx restart

检查和应用

网站配置文件里头部插入

upstream webproxy {
    server IP:端口;  #项目IP和端口,比如127.0.0.1:8888
    server IP:端口;  #项目IP和端口,比如127.0.0.1:8889
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}

负载均衡

将server节点下的location节点中的proxy_pass配置为:http:// + upstream名称,即“
http://webproxy”.

location / { 
    root  html; 
    index  index.html index.htm; 
    proxy_pass http://webproxy; 
}

至此完成网站的简单负载均衡

最后修改:2021 年 01 月 22 日
如果觉得我的文章对你有用,请随意赞赏