首页 > 解决方案 > 如果后端服务器之一关闭,haproxy 将在大约 2-3 秒内出现 404 错误

问题描述

这是 haproxy 配置。

defaults
  option forwardfor
  log global
  option httplog
  log 127.0.0.1 local3
  option  dontlognull
  retries 3
  option redispatch
  timeout connect 5000ms
  timeout client  5000ms
  timeout server 5000ms

listen stats
    bind    *:9000
    mode    http
..............................................
backend testhosts
    mode http
    balance roundrobin
    option httpchk HEAD /sabrix/scripts/menu-common.js
    server host1 11.11.11.11:9080  check inter 2000 rise 1 fall 2
    server host2 11.11.11.12:9080  check inter 2000 rise 1 fall 2

如果 11.11.11.11 的服务宕机,haproxy 会在 2-3 秒左右得到 503 和 404 错误(取决于 inter 值,如果 inter 值非常小,404 错误的数量会减少)。

2020-08-25T11:58:14 11.11.11.11:9080 200 POST /tsturl1 HTTP/1.1 2274
2020-08-25T11:58:14 11.11.11.22:9080 200 POST /tsturl1 HTTP/1.1 448
2020-08-21:5 58:14 11.11.11.11:9080 503 POST /tsturl1 HTTP/1.1 0
2020-08-25T11:58:14 11.11.11.11:9080 404 POST /tsturl1 HTTP/1.1 0
2020-08-25T11:58:14 11.11.111. :9080 200 POST /tsturl1 HTTP/1.1 1503
2020-08-25T11:58:16 11.11.11.22:9080 200 POST /tsturl1 HTTP/1.1 617
2020-08-25T11:58:16 11.11.11.11:9080 /tsturl1 4080 POST HTTP/1.1 0
2020-08-25T11:58:16 11.11.11.22:9080 200 POST /tsturl1 HTTP/1.1 618
2020-08-25T11:58:16 11.11.11.11:9080 404 POST /tsturl1 HTTP/1.1 0
2020-08-25T11:58:16 host1 is DOWN,原因:Layer7错误状态,code:404,info:“Not Found”,检查时长:0ms。剩下 1 台活动服务器和 0 台备用服务器。0 个会话处于活动状态,0 个已重新排队,0 个仍在队列中。2020-08-25T11:58:16 11.11.11.22:9080 200 POST /tsturl1 HTTP/1.1 645
2020-08-25T11:58:16 11.11.11.22:9080 200 POST /tsturl1 HTTP/1.1 618\

我的问题是:为什么参数重试不起作用?即使其中一个后端服务器宕机,用户是否总是得到 200 码而不是 400 错误?我使用的是 Haproxy 1.5.18。

多谢

标签: http-status-code-404backendhaproxy

解决方案


在您使用的版本中retries,用于第 4 层(即连接超时)。HAProxy 2.0 引入了第 7 层重试。这 2 篇博文可能会有所帮助:

HAProxy 2.0

第 7 层重试和混沌工程


推荐阅读