Sharing of 5 weight distribution methods of Nginx upstream

1. Polling (default) 

Each request is allocated to different back-end servers one by one in chronological order. If the back-end server is down, it can be automatically eliminated. 

2. Weight 
specifies the polling probability, which is proportional to the access ratio, and is used when the performance of the back-end server is uneven. 
E.g: 

Copy the code The code is as follows:

upstream backend { 
server 192.168.0.14 weight=10; 
server 192.168.0.15 weight=10; 


3. Each request of ip_hash 
is allocated according to the hash result of accessing the ip, so that each visitor can access a back-end server fixedly, which can solve the problem of session. 
E.g: 

Copy the code The code is as follows:

upstream backend { 
ip_hash; 
server 192.168.0.14:88; 
server 192.168.0.15:80; 


4. Fair (third party) 
allocates requests according to the response time of the back-end server, and priority is given to those with short response time. 

Copy the code The code is as follows:

upstream backend { 
server server1.linuxany.com; 
server server2.linuxany.com; 
fair; 


5、url_hash(第三方) 

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 

例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法 

复制代码代码如下:

upstream backend { 
server squid1:3128; 
server squid2:3128; 
hash $request_uri; 
hash_method crc32; 

#定义负载均衡设备的Ip及设备状态 
upstream backend{ 
ip_hash; 
server 127.0.0.1:9090 down; 
server 127.0.0.1:8080 weight=2; 
server 127.0.0.1:6060; 
server 127.0.0.1:7070 backup; 


在需要使用负载均衡的server中增加 
proxy_pass http://bakend/; 

每个设备的状态设置为: 
1.down 表示单前的server暂时不参与负载 
2.weight 默认为1.weight越大,负载的权重就越大。 
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。 
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。 

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。 

client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug 
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录 

location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

 

########################################

1、轮询

轮询即Round Robin,根据Nginx配置文件中的顺序,依次把客户端的Web请求分发到不同的后端服务器。
配置的例子如下:

http{ 
 upstream sampleapp { 
   server <>; 
   server <>; 
 } 
 .... 
 server{ 
   listen 80; 
   ... 
   location / { 
    proxy_pass http://sampleapp; 
   }  
 } 

上面只有1个DNS入口被插入到upstream节,即sampleapp,同样也在后面的proxy_pass节重新提到。

2、最少连接

Web请求会被转发到连接数最少的服务器上。
配置的例子如下:

http{ 
  upstream sampleapp { 
    least_conn; 
    server <>; 
    server <>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    }  
  } 

上面的例子只是在upstream节添加了least_conn配置。其它的配置同轮询配置。

3、IP地址哈希

前述的两种负载均衡方案中,同一客户端连续的Web请求可能会被分发到不同的后端服务器进行处理,因此如果涉及到会话Session,那么会话会比较复杂。常见的是基于数据库的会话持久化。要克服上面的难题,可以使用基于IP地址哈希的负载均衡方案。这样的话,同一客户端连续的Web请求都会被分发到同一服务器进行处理。
配置的例子如下:

http{ 
  upstream sampleapp { 
    ip_hash; 
    server <>; 
    server <>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    }  
  } 

上面的例子只是在upstream节添加了ip_hash配置。其它的配置同轮询配置。

4、基于权重的负载均衡

基于权重的负载均衡即Weighted Load Balancing,这种方式下,我们可以配置Nginx把请求更多地分发到高配置的后端服务器上,把相对较少的请求分发到低配服务器。
配置的例子如下:

http{ 
  upstream sampleapp { 
    server <> weight=2; 
    server <>; 
  } 
  .... 
  server{ 
    listen 80; 
    ... 
    location / { 
     proxy_pass http://sampleapp; 
    } 
 } 

上面的例子在服务器地址和端口后weight=2的配置,这意味着,每接收到3个请求,前2个请求会被分发到第一个服务器,第3个请求会分发到第二个服务器,其它的配置同轮询配置。

还要说明一点,基于权重的负载均衡和基于IP地址哈希的负载均衡可以组合在一起使用。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326533980&siteId=291194637