非常教程

Nginx参考手册

指南 | Guides

Using nginx as HTTP load balancer

  • 负载平衡方法
  • 默认负载平衡配置
  • 最少连接负载平衡
  • 会话持久性
  • 加权负载平衡
  • 运行检查
  • 进一步阅读

介绍

跨多个应用程序实例进行负载平衡是优化资源利用率,最大化吞吐量,减少延迟并确保容错配置的常用技术。

可以使用nginx作为非常高效的HTTP负载均衡器来将流量分配给多个应用程序服务器,并通过nginx提高Web应用程序的性能,可伸缩性和可靠性。

负载平衡方法

nginx支持以下负载均衡机制(或​​方法):

  • 循环 - 对应用程序服务器的请求以循环方式分发,
  • 最少连接数 - 将下一个请求分配给活动连接数最少的服务器,
  • ip-hash - 哈希函数用于确定下一个请求(基于客户端的IP地址)应该选择哪个服务器。

默认负载平衡配置

使用nginx进行负载平衡的最简单配置可能如下所示:

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

    server {
        listen 80;

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

在上面的示例中,在srv1-srv3上运行相同应用程序的3个实例。如果没有专门配置负载均衡方法,则默认为循环法。所有请求都被代理到服务器组myapp1,并且nginx应用HTTP负载平衡来分发请求。

nginx中的反向代理实现包括HTTP,HTTPS,FastCGI,uwsgi,SCGI和memcached的负载平衡。

要为HTTPS而不是HTTP配置负载均衡,只需使用“https”作为协议。

在为FastCGI,uwsgi,SCGI或memcached设置负载均衡时,分别使用fastcgi_pass,uwsgi_pass,scgi_pass和memcached_pa​​ss指令。

最少连接负载平衡

另一个负载均衡规则是最少连接数的。在某些请求花费较长时间完成的情况下,最少连接允许更公平地控制应用程序实例的负载。

在连接负载最小的情况下,nginx会尽量避免过多请求而使繁忙的应用程序服务器过载,而是将新请求分发给不太繁忙的服务器。

当least_conn指令用作服务器组配置的一部分时,将激活nginx中最少连接的负载平衡:

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

会话持久性

请注意,通过循环或最不连接的负载平衡,每个后续客户端的请求都可能被分发到不同的服务器。不能保证相同的客户端总是定向到相同的服务器。

如果需要将客户端绑定到特定的应用程序服务器 - 换句话说,就始终尝试选择特定服务器而言,要使客户端的会话“粘滞”或“持久” - ip-hash负载平衡机制可以是用过的。

使用ip-hash,客户端的IP地址将用作散列键,以确定应该为客户端请求选择服务器组中的哪台服务器。此方法可确保来自同一客户端的请求始终会定向到同一服务器,除非此服务器不可用。

要配置ip-hash负载平衡,只需将ip_hash指令添加到服务器(上游)组配置:

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

加权负载平衡

通过使用服务器权重,还可以进一步影响nginx负载均衡算法。

在上面的示例中,未配置服务器权重,这意味着所有指定的服务器都被视为具有同等资格的特定负载平衡方法。

特别是循环法,它也意味着服务器之间的请求差不多均等分配 - 只要有足够的请求,并且请求以统一的方式处理并完成得足够快。

当为服务器指定权重参数时,权重将作为负载均衡决策的一部分进行考虑。

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

通过这种配置,每5个新请求将分布在应用程序实例中,如下所示:3个请求将指向srv1,一个请求将转至srv2,另一个请求转至srv3。

在近期的nginx版本中,使用最少连接和ip-hash负载平衡的权重也是类似的可能。

运行检查

nginx中的反向代理实现包括带内(或被动)服务器健康检查。如果来自特定服务器的响应失败并出现错误,则nginx会将此服务器标记为失败,并尝试避免为稍后的入站请求选择此服务器一段时间。

max_fails指令设置在fail_timeout过程中应该发生的与服务器通信的连续失败尝试次数。默认情况下,max_fails设置为1.如果设置为0,则此服务器的运行状况检查被禁用。fail_timeout参数还定义了服务器将被标记为失败的时间。在服务器故障之后的fail_timeout时间间隔之后,nginx将开始正常探测带有活动客户端请求的服务器。如果探针成功,服务器被标记为活动的。

进一步阅读

另外,还有更多控制nginx中服务器负载平衡的指令和参数,例如proxy_next_upstream,backup,down和keepalive。欲了解更多信息,请查阅我们的参考文件

最后一点是,应用程序负载平衡,应用程序运行状况检查,活动监控和服务器组的即时重新配置可作为我们付费NGINX Plus订购的一部分。

以下文章更详细地描述了使用NGINX Plus进行负载平衡:

  • 使用NGINX和NGINX Plus进行负载平衡
  • 使用NGINX和NGINX Plus第2部分进行负载平衡
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module