站长资讯网
最全最丰富的资讯网站

Nginx负载均衡后端健康检查

本次使用第三方模块nginx_upstream_check_module的,要使用这个第三方模块首先您需要进行下载,然后通过patch命令将补丁打入您原有的Nginx源码中,并且重新进行编译安装。

下载nginx_upstream_check_module模块

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

解压

unzip master

Nginx负载均衡后端健康检查

解压后的Nginx和nginx_upstream_check_module-master在同一目录下

Nginx负载均衡后端健康检查

将补丁打入源码(没有patch命令使用yum -y install patch安装)

cd nginx-1.6.3
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch

出现以下代表成功(根据nginx版本选择不同的check)

Nginx负载均衡后端健康检查

编译安装nginx

./configure –prefix=/usr/local/nginx –add-module=../nginx_upstream_check_module-master –with-http_stub_status_module
make && make install

通过以上的步骤,第三方的nginx_upstream_check_module模块就在Nginx中准备好了。接下来我们讲解一下如何使用这个模块。首先看一下upstream的配置信息

  interval:必要参数,检查请求的间隔时间。

  fall:当检查失败次数超过了fall,这个服务节点就变成down状态。

  rise:当检查成功的次数超过了rise,这个服务节点又会变成up状态。

  timeout:请求超时时间,超过等待时间后,这次检查就算失败。

  default_down:后端服务器的初始状态。默认情况下,检查功能在Nginx启动的时候将会把所有后端节点的状态置为down,检查成功后,在置为up。

type:这是检查通信的协议类型,默认为http。以上类型是检查功能所支持的所有协议类型。

一个完整的nginx配置信息如下nginx.conf

worker_processes  4;
error_log  logs/error.log;
events {
    worker_connections  10240;
}
http {
    include      mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream  ekp  { 
    server  10.1.1.131:8080;
    server  10.1.1.132:8080;   
    server  10.1.1.135:8080;   
      check interval=3000 rise=2 fall=5;
      check_keepalive_requests 100;
      check_http_send “HEAD / HTTP/1.1rnConnection: keep-alivernrn”;
      check_http_expect_alive http_2xx http_3xx;
    } 
    server {
        listen      80;
        server_name  www.test.com;
    access_log  /usr/local/nginx/logs/access.log;
        location / {
           
            root  ekp;
            index  index.html index.htm index.jsp;
            proxy_pass        http://ekp;   
            proxy_set_header  Host            $host;
            proxy_set_header  X-Real-IP        $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
        client_max_body_size    200m;
        proxy_connect_timeout      10;
        proxy_read_timeout          300;
        proxy_send_timeout          300;
        }
    location /nstatus {
        check_status;
        access_log on;
      }
    }
}

PS:这里配置按照参考文档加timeout=1000 type=http 否则无法负载均衡

访问测试 http://ip/nstatus

Nginx负载均衡后端健康检查

赞(0)
分享到: 更多 (0)