当前位置: 代码网 > 服务器>服务器>Linux > Nginx负载均衡与健康检查使用详解

Nginx负载均衡与健康检查使用详解

2025年09月29日 Linux 我要评论
一、nginx原生模块介绍我们在使用nginx做反向代理都会使用到以下两个模块:1、ngx_http_proxy_module定义允许将请求传递到另一台服务器。此模块下常用指令如下:proxy_pas

一、nginx原生模块介绍

我们在使用nginx做反向代理都会使用到以下两个模块:

1、ngx_http_proxy_module

定义允许将请求传递到另一台服务器。此模块下常用指令如下:

proxy_pass
proxy_cache
proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout
proxy_next_upstream

2、ngx_http_upstream_module

用于定义可由proxy_pass,fastcgi_pass等指令引用的服务器组。

此模块下常用指令如下:

upstream
server
ip_hash

默认负载均衡配置

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

此时nginx默认的负载均衡策略是轮询外,还有其他默认参数,如下:

http {
    upstream myapp1 {
        server srv1.example.com weight=1 max_fails=1 fail_timeout=10;
        server srv2.example.com weight=1 max_fails=1 fail_timeout=10;
        server srv3.example.com weight=1 max_fails=1 fail_timeout=10;
    }
​
    server {
        listen 80;
        proxy_send_timeout=60;
        proxy_connect_timeout=60;
        proxy_read_timeout=60;
        proxy_next_upstream=error timeout;
​
        location / {
            proxy_pass http://myapp1;
        }
    }
}

二、nginx_upstream_check_module模块

借助淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方realserver的健康状态,如果后端服务器不可用,则会将其踢出upstream,所有的请求不转发到这台服务器。当期恢复正常时,将其加入upstream。

在淘宝自己的tengine上是自带了该模块的,大家可以访问淘宝tengine官网来获取该版本的nginx,也可以到gitbub

如果没有使用淘宝的tengine的话,可以通过补丁的方式来添加该模块到我们自己的nginx中。

下载地址1:https://github.com/yaoweibin/nginx_upstream_check_module

#打补丁
#注意不同版本对应的补丁
cd nginx-1.6.0
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.6 --sbin-path=/usr/local/nginx1.6 --conf-path=/usr/local/nginx1.6/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-pcre=/usr/local/src/nginx/pcre-8.36 --with-zlib=/usr/local/src/nginx/zlib-1.2.8 --add-module=/usr/local/src/nginx/ngx_cache_purge-2.1 --add-module=/usr/local/src/nginx/headers-more-nginx-module-master --add-module=/usr/local/src/nginx/nginx_upstream_check_module-master

make
#不要执行make install命令

cd /usr/local/nginx1.6
#备份命令
cp nginx nginx.bak
nginx -s stop
cp -r /usr/local/src/nginx/nginx-1.6.0/objs/nginx .

打完补丁后,可进行如下配置:

http {

        upstream cluster {

            # simple round-robin

            server 192.168.0.1:80;
            server 192.168.0.2:80;

            check interval=5000 rise=1 fall=3 timeout=4000;

            #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;

            #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
            #check_http_send "head / http/1.0\r\n\r\n";
            #check_http_expect_alive http_2xx http_3xx;
        }

        server {
            listen 80;

            location / {
                proxy_pass http://cluster;
            }

            location /status {
                check_status;

                access_log   off;
                allow some.ip.add.ress;
                deny all;
           }
        }

    }

其中:

syntax:  check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
default: 如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
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。
     
syntax: check_keepalive_requests request_num
default: 1
context: upstream
该指令可以配置一个连接发送的请求数,其默认值为1,表示tengine完成1次请求后即关闭连接。
 
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内传输完成,否则会被健康检查模块视为后端服务器或网络异常。

syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
default: http_2xx | http_3xx
context: upstream
该指令指定http回复的成功状态,默认认为2xx和3xx的状态是健康的。

例子如下:

server{
        listen 80;
        
        upstream test{
            server 192.168.3.12:8080 weight=5 max_fails=3 fail_timeout=10s;
               server 192.168.3.13:8080 weight=5 max_fails=3 fail_timeout=10s;
                
            check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
              check_http_send "head /index.html http/1.0\r\n\r\n";
               check_http_expect_alive http_2xx http_3xx;
       }

        location / {

                proxy_set_header x-real-ip        $remote_addr;
                proxy_set_header x-forwarded-for  $proxy_add_x_forwarded_for;
                proxy_pass http://test;
                proxy_next_upstream error timeout  http_500 http_502 http_503;
        }
    #后端阶段健康状态监控
        location /status {
                check_status;
                access_log off;
        }
}

以上我们同时使用了nginx原生的及淘宝的健康检查模块,但是淘宝的间隔时是毫秒级,而且可以自定义监控url,定制监控页,响应速度快,比原生的敏感度要高。

三、使用阿里巴巴的tengine实现后端节点状态检查

1、安装tengine

unzip tengine-3.1.0.zip
cd tengine-3.1.0/
./configure --prefix=/usr/local/tengine --add-module=/root/tengine-3.1.0/modules/ngx_http_upstream_check_module && make && make install

2、修改配置文件

user  nginx;
worker_processes  1;
error_log  logs/error.log  info;
error_log  "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2g";
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    access_log  "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2g"  main;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    upstream webs {
        
            server 192.168.166.10:80;
            server 192.168.166.13:80;
            check interval=5000 rise=1 fall=3 timeout=4000 type=http default_down=false;
              check_http_send "head /index.html http/1.0\r\n\r\n";
               check_http_expect_alive http_2xx http_3xx;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://webs;
        }
        location /status {
            check_status html;
            access_log   off;
            allow all;
            deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com