我正在尝试使用haproxy来解决一个场景。如下所示
我想这样做,以减少对我的服务器的API调用的数量。有谁能帮我一下吗?
谢谢
发布于 2017-01-04 21:28:05
前两件事很容易,只允许白色的IP
acl whitelist src 10.12.12.23
use_backend SOMESERVER if whitelist第三种-节流-需要使用棍子桌 (有许多数据类型-计数器康涅狄格,塞斯,http,速率.)作为费率计数器:
# max entries count request in 60s periods
stick-table type ip size 200k expire 100s store http_req_rate(60s) 接下来,你必须填满桌子,用跟踪每个请求的例子。通过IP
tcp-request content track-sc0 src
# more info at http://cbonte.github.io/haproxy-dconv/1.5/configuration.html#4.2-tcp-request%20connection最后,acl:
# is there more than 5req/1min from IP
acl http_rate_abuse sc0_http_req_rate gt 5
# update use_backend condition
use_backend SOMESERVER if whitelisted !http_rate_abuse例如,一些具有自定义错误的工作配置文件:
global
log /dev/log local1 debug
defaults
log global
mode http
option httplog
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http
bind *:8181
stick-table type ip size 200k expire 100s store http_req_rate(60s)
tcp-request content track-sc0 src
acl whitelist src 127.0.0.1
acl http_rate_abuse sc0_http_req_rate gt 5
use_backend error401 if !whitelist
use_backend error429 if http_rate_abuse
use_backend realone
backend realone
server local stackoverflow.com:80
# too many requests
backend error429
mode http
errorfile 503 /etc/haproxy/errors/429.http
# unauthenticated
backend error401
mode http
errorfile 503 /etc/haproxy/errors/401.http注意:错误处理有点棘手。由于上面的错误后端缺少服务器条目,haproxy将抛出HTTP503,errorfile捕获它们并发送不同的错误(使用不同的代码)。
示例/etc/haproxy/errors/401.http内容:
HTTP/1.0 401 Unauthenticated
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>401 Unauthenticated</h1>
</body></html>示例/etc/haproxy/errors/429.http内容:
HTTP/1.0 429 Too many requests
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>429 Too many requests</h1>
</body></html>https://stackoverflow.com/questions/41469425
复制相似问题