大网数据广告活动
服务器运维问答

Haproxy balancing - retry with another server if first timed out

· 52CE 我爱测

We are running multiple backend servers with fastcgi protocol and we balance between them using HAproxy. Here is a sample of config: listen Balancer 192.168.0.1:2000 mode tcp option tcplog timeout connect 2000 timeout server 2000…

问题原文

原帖链接 · j99 · CC BY-SA 3.0

We are running multiple backend servers with fastcgi protocol and we balance between them using HAproxy. Here is a sample of config:

listen Balancer 192.168.0.1:2000
    mode tcp
    option tcplog
    timeout connect 2000
    timeout server 2000
    timeout queue 2000
    timeout client 2000
    balance leastconn
    server backend1 192.168.0.2:2000 check inter 2000 rise 2 fall 5
    server backend2 192.168.0.3:2000 check inter 2000 rise 2 fall 5
    server backend3 192.168.0.4:2000 check inter 2000 rise 2 fall 5
    server backend4 192.168.0.5:2000 check inter 2000 rise 2 fall 5
    server backend5 192.168.0.6:2000 check inter 2000 rise 2 fall 5
    server backend6 192.168.0.7:2000 check inter 2000 rise 2 fall 5

The overall timeout is set to 2 seconds but most requests are handled under 0.3 seconds. The problem is that during the peaks sometimes some backend can not reply to a query under 2 seconds and then gateway timeout is returned.

What I would like to do is that when one server is selected (e.g. backend1) and it fails to deliver the reply in 1 second, HAproxy would select another backend and retry. If it fails again in 1 second then the timeout will take place.

So instead of waiting for one server for 2 seconds is it possible to wait 1 second for first, if it fails, try another and then fail?

社区高分解答

原帖链接 · GregL · CC BY-SA 3.0

If I'm reading your question right, using setting timeout server to 1 second (1000 ms), and using option redispatch should give you the desired effect.

option redispatch
no option redispatch
Enable or disable session redistribution in case of connection failure In HTTP mode, if a server designated by a cookie is down, clients may definitely stick to it because they cannot flush the cookie, so they will not be able to access the service anymore.

Specifying "option redispatch" will allow the proxy to break their persistence and redistribute them to a working server.

It also allows to retry last connection to another server in case of multiple connection failures. Of course, it requires having "retries" set to a nonzero value.

This form is the preferred form, which replaces both the "redispatch" and "redisp" keywords.

If this option has been enabled in a "defaults" section, it can be disabled in a specific instance by prepending the "no" keyword before it.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-06-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。