论坛首页 综合技术论坛

nginx upstream 调度策略

浏览 7028 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (3)
作者 正文
   发表时间:2011-08-17  

之前一直使用nginx 的upstream,今天有个哥们问我,upstream的调动算法是什么,我说我还真没注意过,使用Haproxy的时候倒是注意过,回来一查,原来也是round-robin,下面是nginx 官方文档给出的说明:

 


This module provides simple load-balancing (round-robin and client IP) across backend servers.

Example:

upstream

 backend  {

  server

 backend1.example.com weight=
5
;

  server

 backend2.example.com:8080
;

  server

 unix:/tmp/backend3;

}

 
server

 {

  location

 / {

    proxy_pass

  http

://backend;

  }

}

 

 

轮询调度算法(Round-Robin Scheduling)

  轮询调度算法的原理是每一次把来自用户的请求轮流分配给内部中的服务器,从1开始,直到N(内部服务器个数),然后重新开始循环。

  算法的优点是其简洁性,它无需记录当前所有连接的状态,所以它是一种无状态调度。

  轮询调度算法流程

  假设有一组服务器N台,S = {S1, S2, …, Sn},一个指示变量i表示上一次选择的服务器ID。变量i被初始化为N-1。其算法如下:

 

j = i;

do

{

j = (j + 1) mod n;

i = j;

return Si;

} while (j != i);

return NULL;

 

 

当然nginx也不止round-robin 这一种策略还有ip_hash 一种,这种好处就是一个ip总是被转发同一server

 

ip_hash

syntax: ip_hash

default: none

context: upstream

This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.

It is not possible to combine ip_hash and weight methods for connection distribution. If one of the servers must be removed for some time, you must mark that server as *down*.

For example:

upstream

 backend {

  ip_hash

;

  server

   backend1.example.com;

  server

   backend2.example.com;

  server

   backend3.example.com  down;

  server

   backend4.example.com;

}

 

   发表时间:2011-08-24  
随机, 一致性, 源地址, 轮询, 平均负载 就这么几个简单算法.  
0 请登录后投票
   发表时间:2011-08-26  
Nginx的upstream目前支持5种方式的分配
1 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2 weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend { server 192.168.0.88 weight=10; server 192.168.0.89 weight=10; }

3 ip_hash


每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以
解决session的问题。
例如:
upstream bakend { ip_hash; server 192.168.0.88:80; server 192.168.0.89:80; }

4 fair(第三方)


按后端服务器的响应时间来分配请求,响应时间短的优先分配。
例如:
upstream bakend { server 192.168.0.88:80; server 192.168.0.89:80; fair; }

5 url_hash(第三方)


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

upstream backend { server 192.168.0.88:3128; server 192.168.0.89:3128; hash $request_uri; hash_method crc32; }

0 请登录后投票
   发表时间:2011-08-29  
greatwqs 写道
Nginx的upstream目前支持5种方式的分配
1 轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2 weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend { server 192.168.0.88 weight=10; server 192.168.0.89 weight=10; }

3 ip_hash


每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以
解决session的问题。
例如:
upstream bakend { ip_hash; server 192.168.0.88:80; server 192.168.0.89:80; }

4 fair(第三方)


按后端服务器的响应时间来分配请求,响应时间短的优先分配。
例如:
upstream bakend { server 192.168.0.88:80; server 192.168.0.89:80; fair; }

5 url_hash(第三方)


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

upstream backend { server 192.168.0.88:3128; server 192.168.0.89:3128; hash $request_uri; hash_method crc32; }


受教了,谢谢分享 :)
0 请登录后投票
   发表时间:2011-08-30  
这个好。。。受教。。。
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics