Introduction to several algorithms of NIGNX load Balancing

Source: Internet
Author: User
Tags crc32 nginx load balancing

One, Nginx load balancing algorithm

1、轮询(默认)    每个请求按时间顺序逐一分配到不同的后端服务,如果后端某台服务器死机,自动剔除故障系统,使用户访问不受影响。2、weight(轮询权值)    weight的值越大分配到的访问概率越高,主要用于后端每台服务器性能不均衡的情况下。或者仅仅为在主从的情况下设置不同的权值,达到合理有效的地利用主机资源。3、ip_hash    每个请求按访问IP的哈希结果分配,使来自同一个IP的访客固定访问一台后端服务器,并且可以有效解决动态网页存在的session共享问题。4、fair    比 weight、ip_hash更加智能的负载均衡算法,fair算法可以根据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间 来分配请求,响应时间短的优先分配。Nginx本身不支持fair,如果需要这种调度算法,则必须安装upstream_fair模块。5、url_hash    按访问的URL的哈希结果来分配请求,使每个URL定向到一台后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身不支持url_hash,如果需要这种调度算法,则必须安装Nginx的hash软件包。

First, polling (default)

Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.

Second, weight

Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven.
For example:

Copy the code code as follows:
Upstream Bakend {
Server 192.168.0.14 weight=10;
Server 192.168.0.15 weight=10;
}

Third, Ip_hash

Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.
For example:

Copy the code code as follows:
Upstream Bakend {
Ip_hash;
Server 192.168.0.14:88;
Server 192.168.0.15:80;
}

Iv. Fair (third party)

The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.

Copy the code code as follows:
Upstream Backend {
server Server1;
Server Server2;
Fair
}

V. Url_hash (Third Party)

Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.
Example: Add a hash statement in upstream, the server statement can not write weight and other parameters, Hash_method is the use of the hash algorithm

Copy the code code as follows:
Upstream Backend {
Server squid1:3128;
Server squid2:3128;
Hash $request _uri;
Hash_method CRC32;
Second, Nginx load balancing scheduling state

In the Nginx upstream module, you can set the state of each back-end server in load balancing scheduling, and the commonly used states are:

1、down,表示当前的server暂时不参与负载均衡2、backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的访问压力最低3、max_fails,允许请求失败的次数,默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误。4、fail_timeout,请求失败超时时间,在经历了max_fails次失败后,暂停服务的时间。max_fails和fail_timeout可以一起使用。

If Nginx does not only have to proxy a server, it is not as hot as today, Nginx can be configured to proxy multiple servers, when a server down, can still keep the system available. The specific configuration process is as follows:

    1. Under the HTTP node, add the upstream node.

Upstream LINUXIDC {
Server 10.0.6.108:7080;
Server 10.0.0.85:8980;
}

    1. Configure the Proxy_pass in the location node under the server node to be://+ upstream name, i.e. "
      HTTP://LINUXIDC ".

Location/{
root HTML;
Index index.html index.htm;
Proxy_pass HTTP://LINUXIDC;
}

3.  现在负载均衡初步完成了。upstream按照轮询(默认)方式进行负载,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。虽然这种方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。适用于图片服务器集群和纯静态页面服务器集群。除此之外,upstream还有其它的分配策略,分别如下:weight(权重)指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。如下所示,10.0.0.88的访问比率要比10.0.0.77的访问比率高一倍。

Upstream linuxidc{
Server 10.0.0.77 weight=5;
Server 10.0.0.88 weight=10;
}

ip_hash(访问ip)每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

Upstream favresin{
Ip_hash;
Server 10.0.0.10:8080;
Server 10.0.0.11:8080;
}

fair(第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。与weight分配策略类似。

Upstream favresin{
Server 10.0.0.10:8080;
Server 10.0.0.11:8080;
Fair
}

Url_hash (third Party)

Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.

Note: In upstream the hash statement is added, the server statement cannot write other parameters such as weight, Hash_method is the hash algorithm used.

Upstream resinserver{
Server 10.0.0.10:7777;
Server 10.0.0.11:8888;
Hash $request _uri;
Hash_method CRC32;
}

Upstream can also set a status value for each device, with the meanings of these status values as follows:

Down indicates that the server before the single is temporarily not participating in the load.

Weight by default, the larger the 1.weight, the greater the load weight.

Max_fails: The number of times that a request failed is allowed defaults to 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded.

Fail_timeout:max_fails the time of the pause after the failure.

Backup: When all other non-backup machines are down or busy, request the backup machine. So the pressure on this machine is the lightest.

Upstream bakend{#定义负载均衡设备的Ip及设备状态
Ip_hash;
Server 10.0.0.11:9090 down;
Server 10.0.0.11:8080 weight=2;
Server 10.0.0.11:6060;
Server 10.0.0.11:7070 backup;
}

Introduction to several algorithms of NIGNX load Balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.