Nginx_upstream 負載均衡&高可用
負載均衡調度算法:
upstream duanserver { server 192.168.4.41:80 weight=1 max_fails=3 fail_timeout=10s; server 192.168.4.42:80 weight=2 max_fails=3 fail_timeout=10s; } server { listen 80; server_name www.xxx.com; root /usr/local/nginx/html; location / { root html; index index.html index.htm; proxy_pass http://duanserver; #後端服務出現以下錯誤情況,實現故障轉移(可自動切走 & 切回); proxy_next_upstream http_403 http_404 http_500 http_502 http_503 error timeout invalid_header; include /usr/local/nginx/conf/proxy.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
輪詢:請求逐一分配,後端宕機自動剔除;
Weight:權值輪詢
ip_hash:按來源IP hash結果分配,同一IP訪客固定訪問一個後端server,解決動態網頁session共享問題
fair:智能算法(根據頁面大小&加載時間智能選擇,後端處理時間短的優先分配),需加載upstream_fair模塊,高消耗性能
url_hash:訪問url hash結果分配,每個url固定定向到一個後端server,提高後端效率,(需加載nginx hash 軟件包)
定義後端服務器狀態:
down:不參與負載
backup:預留機,(當所有非backup故障或繁忙時啓用)
max_fails:允許請求失敗次數
failtimeout:在經過maxfails次失敗後,暫停服務時間,一起使用
當調度算法ip_hash ,狀態不能是backup & weight
Any question, please contact me!