Nginx upstream 的几种分配方式

     一、轮询(默认)

    每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端服务器down掉,能自动剔除。 html

    二、weight linux

    指定轮询概率,weight和访问比率成正比,用于后端服务器性能不均的状况。 nginx

    例如: 算法

    upstream bakend { 后端

    server 192.168.1.10 weight=10; 缓存

    server 192.168.1.11 weight=10; 服务器

    } session

    三、ip_hash 负载均衡

    每一个请求按访问ip的hash结果分配,这样每一个访客固定访问一个后端服务器,能够解决session的问题。 post

    例如:

    upstream resinserver{

    ip_hash;

    server 192.168.1.10:8080;

    server 192.168.1.11:8080;

    }

    四、fair(第三方)

    按后端服务器的响应时间来分配请求,响应时间短的优先分配。

    upstream resinserver{

    server server1;

    server server2;

    fair;

    }

    五、url_hash(第三方)

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

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

    upstream resinserver{

    server squid1:3128;

    server squid2:3128;

    hash $request_uri;

    hash_method crc32;

    }

    tips:

    upstream resinserver{#定义负载均衡设备的Ip及设备状态

    ip_hash;

    server 127.0.0.1:8000 down;

    server 127.0.0.1:8080 weight=2;

    server 127.0.0.1:6801;

    server 127.0.0.1:6802 backup;

    }

    在须要使用负载均衡的server中增长

    proxy_pass http://resinserver/;

    每一个设备的状态设置为:

    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进行匹配.能够进行重定向或者进行新的代理 负载均衡


推荐阅读:关于负载均衡

一、LVS集群中的IP负载均衡技术——章文嵩(淘宝正明)

http://www.linuxvirtualserver.org/zh/lvs3.html

二、集群LVS的十种调度算法

http://www.linuxany.com/archives/1270.html

三、Nginx开发从入门到精通

http://tengine.taobao.org/book/index.html

四、解析nginx负载均衡  

http://stblog.baidu-tech.com/?p=2027

相关文章
相关标签/搜索