测试环境

nginx-1.10.0

负载均衡原理

客户端向反向代理发送请求,接着反向代理根据某种负载机制转发请求至目标服务器(这些服务器都运行着相同的应用),并把获得的内容返回给客户端,期中,代理请求可能根据配置被发往不同的服务器。

nginx负载均衡轮询 nginx 负载均衡原理_服务器

 

 

负载均衡配置

测试案例:

 

如下,分别在两台服务器(192.168.1.103, 192.168.1.102)上部署了相同的应用,并通过8080端口访问网站,如下

 

http://192.168.1.xx:8080/webautotest/xxxxxxx

 

同时在192.168.1.103上安装了nginx反向代理,想通过192.168.1.103的80端口来实现对两个站点的访问

nginx负载均衡轮询 nginx 负载均衡原理_nginx_02

 

nginx负载均衡轮询 nginx 负载均衡原理_nginx负载均衡轮询_03

 

 

 

编辑nginx配置文件(例中为/usr/local/ngnix/conf/nginx.conf),找到http结点,如下,添加带背景色部分的内容,

http {
    upstream myapp1 {
        server 192.168.1.103:8080;
        server 192.168.1.104:8080;
    }
     ……略
   server {
        listen 80;
        server_name  localhost;
 
         ……略
        location /webautotest/ {
            proxy_buffering off;
            proxy_pass http://myapp1;
        }
    }
}
 
 重新加载配置文件
[root@localhost nginx-1.10.0]# /usr/local/ngnix/sbin/nginx -s reload

 

访问测试url

如下,访问相同的页面,展示来自不同服务器的页面

 

nginx负载均衡轮询 nginx 负载均衡原理_nginx负载均衡轮询_04

 

nginx负载均衡轮询 nginx 负载均衡原理_服务器_05

 

 

说明:

负载均衡方法

nginx提供了以下三种负载均衡机制、方法:

  • round-robin — 请求以循环、轮转的方式分发到应用服务器。
  • least-connected — 下一个请求被分配到拥有最少活动连接数的服务器
  • ip-hash — 使用一个哈希函数,基于客户端ip地址判断下一个请求应该被分发到哪个服务器。

 

默认的负载均衡配置

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }
 
    server {
        listen 80;
 
        location / {
            proxy_pass http://myapp1;
        }
    }
}

上例中,有3个应用实例分别运行在srv1-srv3。当不显示指定负载均衡方法时,默认为round-robin。所有请求都被代理转发至myapp1服务器组,并根据负载均衡方法来分发请求。

 

最少连接负载均衡

另一个负载均衡原则为least-connected。当一些请求花费较长时间来完成时,least-connected更“公平”的控制应用程序实例上的负载。

 

配置了least-connected的负载均衡机制的情况下,nginx会尽量不让负载繁忙的应用服务器上负载过多的请求,相反的,会把新的请求发送到比较不繁忙的服务器。

 

配置示例:

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

 

 

会话持久性

注意,round-robin或least-connected负载均衡下,每个后续的客户端可能被分发至不同服务器,不保证相同客户端的请求总是被发送到相同的服务器。

 

如果有必要把客户端绑定至特定服务器,则可使用ip-hash负载均衡机制。

 

ip-hash机制下,客户端ip地址被用作hash key来判断客户端请求应该发送到哪个服务器,这种方法保证了来自相同客户端的请求总是发送到相同服务器(如果服务器可用的话)

 

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

 

负载均衡权重

可通过配置服务器权重来影响负载均衡机制。

 

上面的例子中,都未配置服务器权重,这意味着所有服务器都拥有相同的权重。

 

针对round-robin负载机制,权重意味着更多或更少的请求传送至服务器---假设有足够的请求,且按统一方式处理请求,且足够快完成请求处理。

 

配置示例:
  upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

 

上例配置中,每发送至服务器实例的5个新的请求中,有3个发送到srv1,1个发送到srv2,另1个发送到srv3。

 

注:当前版本似乎只实现了round-robin机制下的权重设置

 

健康检测

nginx反向代理实现包含服务器健康检查。如果来自特定服务器的响应失败,报错,nginx将标记该服务器为failed,一段时间内尽量避免选择此服务器作为随后请求的分发服务器。

 

max_fails机制设置fail_timeout期间,和服务器沟通失败的连续重试次数,默认为1.当设置为0时,不做服务器健康检测。fail_timeout定义了服务器被标记为failed的时长。fail_timeout时间间隔过后,nginx将开始使用活动客户端请求来探测服务器,如果探测成功则标记服务器为活动服务器。

 

进一步阅读

In addition, there are more directives and parameters that control server load balancing in nginx, e.g. proxy_next_upstream, backup, down, and keepalive. For more information please check our reference documentation.

Last but not least, application load balancing, application health checks, activity monitoring and on-the-fly reconfiguration of server groups are available as part of our paid NGINX Plus subscriptions.

The following articles describe load balancing with NGINX Plus in more detail: