Vie*_*ham 5 nginx rate-limiting
我正在尝试根据响应状态代码限制 nginx 的请求。我想减慢具有太多 4xx 或 5xx 响应的请求。我的配置文件中有这个代码块:
map $status $bad_guy {
~^[23] "";
default $binary_remote_addr;
}
limit_req_zone "$bad_guy" zone=badguy:10m rate=1r/s;
server {
limit_req zone=badguy burst=20;
Run Code Online (Sandbox Code Playgroud)
看来上面的配置阻止了所有发送超过 1 rps 的 IP 地址,包括那些只有 200 OK 响应的 IP 地址。
请问你能帮帮我吗?为什么上面的配置不起作用?我是否必须使用其他东西(也许是 openresty?)来实现这一目标?谢谢。
这非常棘手,因为声明 limit_req_zone 时 $status 变量为空。$status 仅在 nginx 处理完请求后才知道。例如在 proxy_pass 指令之后。
我最接近实现状态速率限制的方法是执行以下操作:
...
...
...
limit_req_zone $binary_remote_addr zone=api:10m rate=5r/s;
...
...
...
server {
location /mylocation {
proxy_intercept_errors on;
proxy_pass http://example.org;
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @custom_error;
}
location @custom_error {
limit_req zone=api burst=5 nodelay;
return <some_error_code>;
}
}
...
Run Code Online (Sandbox Code Playgroud)
缺点是,通过这种方式,您必须返回与代理传递响应不同的状态代码。
归档时间: |
|
查看次数: |
3012 次 |
最近记录: |