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!
本文出自 “Rookie on the road” 博客,请务必保留此出处http://duanyifei.blog.51cto.com/9618411/1941581
原文地址:http://duanyifei.blog.51cto.com/9618411/1941581