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

Nginx rate limiting - only slowdown if ceiling hit

· 52CE 我爱测

We recently switched to nginx and I was happy to see that rate limiting seems easy enough there. I set up the following limit for a particular zone: limit_req_zone $binary_remote_addr zone=PHPUtilities:10m rate=60r/m; I applied t…

问题原文

原帖链接 · Mike Willis · CC BY-SA 4.0

We recently switched to nginx and I was happy to see that rate limiting seems easy enough there. I set up the following limit for a particular zone:

limit_req_zone $binary_remote_addr zone=PHPUtilities:10m rate=60r/m;

I applied that zone to a location block with a burst of 20. That all seemed fine, but what I was surprised to see was that I would quickly get slowdowns where the server would refuse to respond for 5 seconds or so. It seems like nginx takes the 60 requests per minute and divides that down to 1 request per second. So if we loaded several scripts simultaneously, nginx would serve the first request right away and then delay the others.

I'd rather it behaved sort of like "ok they get a free 60 requests per minute. Only once they hit 60 will we begin limiting them, first by slowing down the next 20 requests (the burst). Everything after that will crap out until they stop requesting so much, or fall below the 60 per minute limit."

Is there a way to do that?

PS. I accidentally asked this question on Stack Overflow first, then realized it was more appropriate here. :(

已采纳解答

原帖链接 · Daniel Compton · CC BY-SA 4.0

I think you want to use the nodelay parameter as well as burst:

location /login/ {
    limit_req_zone $binary_remote_addr zone=PHPUtilities:10m rate=60r/m burst=20 nodelay;

    proxy_pass http://my_upstream;
}

With the nodelay parameter, NGINX still allocates slots in the queue according to the burst parameter and imposes the configured rate limit, but not by spacing out the forwarding of queued requests. Instead, when a request arrives “too soon”, NGINX forwards it immediately as long as there is a slot available for it in the queue. It marks that slot as “taken” and does not free it for use by another request until the appropriate time has passed (in our example, after 100 milliseconds).

From https://www.nginx.com/blog/rate-limiting-nginx/.

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

资料来源与适用范围

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

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

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