Using nginx as HTTP load balancer使用nginx作为HTTP负载平衡器

Load balancing methods负载平衡方法
Default load balancing configuration默认负载平衡配置
Least connected load balancing最小连接负载平衡
Session persistence会话持续
Weighted load balancing加权负载平衡
Health checks健康检查
Further reading进一步阅读

Introduction介绍

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.跨多个应用程序实例的负载平衡是优化资源利用率、最大化吞吐量、减少延迟和确保容错配置的常用技术。

It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.可以使用nginx作为非常高效的HTTP负载平衡器,将流量分配到多个应用服务器,并使用nginx提高web应用程序的性能、可伸缩性和可靠性。

Load balancing methods负载平衡方法

The following load balancing mechanisms (or methods) are supported in nginx:nginx支持以下负载平衡机制(或方法):

Default load balancing configuration默认负载平衡配置

The simplest configuration for load balancing with nginx may look like the following:使用nginx进行负载平衡的最简单配置可能如下所示:

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

server {
listen 80;

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

In the example above, there are 3 instances of the same application running on srv1-srv3. 在上面的示例中,同一应用程序有3个实例在srv1-srv3上运行。When the load balancing method is not specifically configured, it defaults to round-robin. 如果未具体配置负载平衡方法,则默认为循环。All requests are proxied to the server group myapp1, and nginx applies HTTP load balancing to distribute the requests.所有请求都被代理到服务器组myapp1,nginx应用HTTP负载平衡来分发请求。

Reverse proxy implementation in nginx includes load balancing for HTTP, HTTPS, FastCGI, uwsgi, SCGI, memcached, and gRPC.nginx中的反向代理实现包括HTTP、HTTPS、FastCGI、uwsgi、SCGI、memcached和gRPC的负载平衡。

To configure load balancing for HTTPS instead of HTTP, just use “https” as the protocol.要为HTTPS而不是HTTP配置负载平衡,只需使用“HTTPS”作为协议。

When setting up load balancing for FastCGI, uwsgi, SCGI, memcached, or gRPC, use fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass directives respectively.为FastCGI、uwsgi、SCGI、memcached或gRPC设置负载平衡时,分别使用fastcgi_passuwsgi_passscgi_passmemcached_passgrpc_pass指令。

Least connected load balancing最小连接负载平衡

Another load balancing discipline is least-connected. 另一个负载平衡规程是连接最少的。Least-connected allows controlling the load on application instances more fairly in a situation when some of the requests take longer to complete.连接最少允许在某些请求需要更长时间才能完成的情况下更公平地控制应用程序实例上的负载。

With the least-connected load balancing, nginx will try not to overload a busy application server with excessive requests, distributing the new requests to a less busy server instead.通过连接最少的负载平衡,nginx将尽量不让繁忙的应用程序服务器过载,而是将新请求分发到不太繁忙的服务器。

Least-connected load balancing in nginx is activated when the least_conn directive is used as part of the server group configuration:least_conn指令用作服务器组配置的一部分时,nginx中的最小连接负载平衡将被激活:

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

Session persistence会话持续

Please note that with round-robin or least-connected load balancing, each subsequent client’s request can be potentially distributed to a different server. 请注意,通过循环或连接最少的负载平衡,每个后续客户端的请求都可能被分发到不同的服务器。There is no guarantee that the same client will be always directed to the same server.无法保证同一客户机始终指向同一服务器。

If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the ip-hash load balancing mechanism can be used.如果需要将客户机绑定到特定的应用程序服务器—换句话说,就始终尝试选择特定的服务器而言,使客户机的会话“粘性”或“持久性”—可以使用ip哈希负载平衡机制。

With ip-hash, the client’s IP address is used as a hashing key to determine what server in a server group should be selected for the client’s requests. 使用ip-hash,客户端的ip地址用作哈希键,以确定应为客户端请求选择服务器组中的哪个服务器。This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.此方法确保来自同一客户端的请求将始终定向到同一服务器,除非该服务器不可用。

To configure ip-hash load balancing, just add the ip_hash directive to the server (upstream) group configuration:要配置ip哈希负载平衡,只需将ip_hash指令添加到服务器(上游)组配置:

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

Weighted load balancing加权负载平衡

It is also possible to influence nginx load balancing algorithms even further by using server weights.通过使用服务器权重,还可以进一步影响nginx负载平衡算法。

In the examples above, the server weights are not configured which means that all specified servers are treated as equally qualified for a particular load balancing method.在上面的示例中,未配置服务器权重,这意味着所有指定的服务器都被视为具有同等资格的特定负载平衡方法。

With the round-robin in particular it also means a more or less equal distribution of requests across the servers — provided there are enough requests, and when the requests are processed in a uniform manner and completed fast enough.特别是循环,它还意味着请求在服务器上的分布或多或少是平等的——只要有足够的请求,并且请求以一致性的方式处理并且完成得足够快。

When the weight parameter is specified for a server, the weight is accounted as part of the load balancing decision.为服务器指定权重参数时,权重将作为负载平衡决策的一部分进行计算。

upstream myapp1 {
        server srv1.example.com weight=3;
        server srv2.example.com;
        server srv3.example.com;
    }

With this configuration, every 5 new requests will be distributed across the application instances as the following: 3 requests will be directed to srv1, one request will go to srv2, and another one — to srv3.通过此配置,每5个新请求将分布在应用程序实例中,如下所示:3个请求将定向到srv1,一个请求将定向到srv2,另一个请求将定向到srv3。

It is similarly possible to use weights with the least-connected and ip-hash load balancing in the recent versions of nginx.在最新版本的nginx中,同样可以使用连接最少的权重和ip哈希负载平衡。

Health checks健康检查

Reverse proxy implementation in nginx includes in-band (or passive) server health checks. If the response from a particular server fails with an error, nginx will mark this server as failed, and will try to avoid selecting this server for subsequent inbound requests for a while.nginx中的反向代理实现包括带内(或被动)服务器健康检查。如果来自特定服务器的响应因错误而失败,nginx会将此服务器标记为失败,并在一段时间内尝试避免为后续入站请求选择此服务器。

The max_fails directive sets the number of consecutive unsuccessful attempts to communicate with the server that should happen during fail_timeout. max_fails指令设置在fail_timeout期间与服务器通信的连续失败尝试次数。By default, max_fails is set to 1. 默认情况下,max_fails设置为1。When it is set to 0, health checks are disabled for this server. 将其设置为0时,将禁用此服务器的运行状况检查。The fail_timeout parameter also defines how long the server will be marked as failed. fail_timeout参数还定义服务器标记为失败的时间。After fail_timeout interval following the server failure, nginx will start to gracefully probe the server with the live client’s requests. 在服务器发生故障后的fail_timeout间隔之后,nginx将开始用实时客户端的请求优雅地探测服务器。If the probes have been successful, the server is marked as a live one.如果探测成功,服务器将被标记为活动服务器。

Further reading进一步阅读

In addition, there are more directives and parameters that control server load balancing in nginx, e.g. proxy_next_upstream, backup, down, and keepalive. 此外,nginx中还有更多控一致性务器负载平衡的指令和参数,例如proxy_next_upstreambackupdownkeepaliveFor 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.最后但并非最不重要的一点是,应用程序负载平衡应用程序健康检查活动监视和服务器组的动态重新配置都可以作为我们的付费NGINX Plus订阅的一部分提供。

The following articles describe load balancing with NGINX Plus in more detail:以下文章更详细地介绍了NGINX Plus的负载平衡: