利用Nginx分发请求到多个后端Server达到负载均衡的效果,有时Server异常,Nginx依然会分发到这个异常Server,导致服务出现偶然性不可用;
为了避免这个问题,Nginx提供了简单的容灾机制,在后端Server出现异常时,会分发到另外一个服务正常的Server,配置文件如下
upstream cgi-bin{ server 10.123.45.78:8080 weight=1 max_fails=2 fail_timeout=10s; server 10.123.45.79:8080 weight=1 max_fails=2 fail_timeout=10s; } location /cgi-bin { proxy_connect_timeout 10s; proxy_read_timeout 10s; proxy_next_upstream http_404 http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_pass http://cgi-bin; }
upstream 参数解释
weight:
请求分发的权重,默认值 1
max_fails:
在规定时间内(fail_timeout)检测Server是否可用,如果失败次数超过max_fails,则标记该Server为不可用,Nginx不会分发请求到该Server,直到在下次检测健康为止;
健康检测通过 proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, memcached_next_upstream 来判断;
默认值 1, 设置为 0 则不进行健康检测;
fail_timeout:
健康检测的频率,Server不可用的时间;
默认值 10s;
location 参数解释
proxy_connect_timeout:
与Server握手超时时间
proxy_read_timeout:
等待Server返回结果的超时时间
proxy_next_upstream:
捕捉Server可能出现的异常,如果出现则标志该Server为不可用,Nginx分发请求到下一个服务正常的Server
Leave a Reply