在负载反向代理的算法上,共有五种:轮询、ip_hash、weight、fair(第三方)、url_hash(第三方)

轮询是默认的,每一个请求按顺序逐一分配到不同的后端服务器,如果后端服务器down掉了,则能自动剔除。(如果又存活了,能自动加进来吗?)

配置文件如下:

 upstream test {
server 192.168.109.5:81;
servse 192.168.109.3:80;
}
测试结果:
[root@xxw conf]# for i in $(seq 10); do curl
http://192.168.109.4  ;done192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.5  It is work!192.168.109.3 It is work!
weight是设置权重,用于后端服务器性能不均的情况,访问比率约等于权重之比
配置文件:upstream test {
server 192.168.109.5:81 ;
servse 192.168.109.3:80 weight=5;
}
测试结果:
[root@xxw conf]# for i in $(seq 10); do curl
http://192.168.109.4  ;done192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.3 It is work!192.168.109.3 It is work!192.168.109.3 It is work!192.168.109.3 It is work!192.168.109.5  It is work!192.168.109.3 It is work!192.168.109.3 It is work!192.168.109.3 It is work!
测试结果一直是访问这个1次,再访问那个5次,反复循环。
ip_hash 解决了session问题:每个请求按访问IP的hash结果分配,这样每个访客可以固定一个后端服务器。
配置文件:  upstream test {
           ip_hash;
server 192.168.109.5:81;
servse 192.168.109.3:80;
}

测试结果:
[root@xxw conf]# for i in $(seq 10); do curl
http://192.168.109.4  ;done192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!192.168.109.5  It is work!
fair (第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。
配置文件:upstream test {
server 192.168.109.5:81;
servse 192.168.109.3:80;
fair;
}


url_hash  (第三方) 按访问URL的hash结果来分配请求,使每个URL定向到同一个后端服务器,后端服务器为缓存时比较适用。另外,在upstream中加入hash语句后,server语句不能写入weight等其他参数。
配置文件:upstream test {
server squidIP1:3128;
servse squidIP2:3128;
           hash  $request_uri;
           hash_method  crc32;
        }

 upstream test {
           ip_hash;
server 192.168.109.5:81;down
servse 192.168.109.4:80 weight=2 max_fails=2 fail_timeout=30s;
servse 192.168.109.3:80 backup;

}

#down 暂时不参与负载
#weight 权重
#max_fail 允许请求失败的次数,默认为1
#fail_timeout max_fail次失败后,暂停的时间
#backup 所有其他非backup机器down或者忙的时候,才请求backup。所以这台机器压力最轻。


location / {
   proxy_pass      
http://test /;   #将请求传到负载服务器池
        }
root 指令:指定目录
proxy_pass  proxy_store  proxy_cache :实现缓存代理
expires  指定缓存时间