请问nginx如何设置负载均衡和失败次数?
网友回复
upstream mysvr { ip_hash; server 127.0.0.1:7878 weight=2 max_fails=2 fail_timeout=2; server 192.168.1.222:7878 weight=2 max_fails=2 fail_timeout=2; server 192.168.10.121:3333 weight=1 max_fails=2 fail_timeout=1; server 192.168.10.121:3333 backup; } server{ ...... location / { proxy-pass http://mysvr; #请求转向mysvr 定义的服务器列表 } }
p_hash:nginx会让相同的客户端ip请求相同的服务器
down,表示当前的server暂时不参与负载均衡。
backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。