原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本聲明。不然將追究法律責任。http://youboy.blog.51cto.com/6090216/1009330算法
在負載反向代理的算法上,共有五種:輪詢、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 指定緩存時間
後端
本文出自 「仰望青春-李寒斌」 博客,請務必保留此出處http://youboy.blog.51cto.com/6090216/1009330緩存