如何使用HAProxy通过HTTP状态代码对限制进行评分?

时间:2019-05-07 04:07:20

标签: haproxy

HAProxy提供一个内置的http_err_rate计数器,该计数器“报告该时间段内的平均HTTP请求错误率。”可以在stick表中使用它来限制产生大量错误的客户端的速率。可能看起来像这样:

frontend web
  tcp-request content reject if { src_get_gpc0(Abuse) gt 0 }
  acl scanner src_http_err_rate(Abuse) ge 10
  http-request deny if scanner flag_abuser

backend Abuse
  stick-table type ip size 1m expire 60m store gpc0,http_err_rate(20s)

我想做的是跟踪http_err_rate之类的东西,但只跟踪401 Unauthorized状态码。这样,HAProxy将只关心限制未授权请求的速率,而不是所有HTTP错误代码。

谢谢!

2 个答案:

答案 0 :(得分:0)

  

我想做的是跟踪http_err_rate之类的内容,但只能跟踪401未经授权的状态代码。

您可以将General Purpose Counters与在status提取中匹配的ACL一起使用。以下示例配置将跟踪给定IP地址[1]的404错误率,如果每10秒超过10个请求,则以429状态拒绝请求:

frontend fe_http
    mode http
    bind *:8080

    stick-table type ipv6 size 10k expire 300s store gpc0_rate(10s)
    http-request  track-sc0 src
    http-request  deny deny_status 429 if { sc0_gpc0_rate gt 10 }
    # Relevant line below
    http-response sc-inc-gpc0(0) if { status 404 }

    default_backend be_http

backend be_http
    mode http
    server example example.com:80

[1]注意:我建议使用ipv6作为摇杆表密钥,它可能同时包含IPv4和IPv6地址。

答案 1 :(得分:0)

如果要根据其401速率进行速率限制,则需要在配置中将401代码更改为401:

http-request deny deny_status 401 if { sc_http_req_cnt(0) gt 10 }
  

同时使用deny和tarpit,您可以添加deny_status标志来设置   自定义响应代码,而不是他们用完的默认403/500   的盒子。例如,使用http-request deny deny_status 429将   导致HAProxy以错误429响应客户端:太多   请求。

有关ACL和速率限制的更多“常规”信息,您可以查看:

https://www.haproxy.com/blog/four-examples-of-haproxy-rate-limiting/ https://www.haproxy.com/blog/introduction-to-haproxy-acls/