Nginx limit_req_zone ignores rate value

nginxrate-limiting

I have a strange behavior my config looks something like this:

http {
    limit_req_zone $binary_remote_addr zone=nocachelimit:10m rate=120r/m;
}
...
location "/api" {
    {
        limit_req zone=nocachelimit burst=20;
    }
}

Regardless of what values i put into the rate, I even tried 1000r/s
I keep getting log entries for "normal" users that say:

 [warn] 16526#0: *4661 delaying request, excess: 1.000, by zone "nocachelimit"

The typical usecase is a Pageload that does not fall into the zone + 4-5 Ajax requests that will should be limited to the zone.

What could be the cause for this kind of behavior.

Best Answer

The rate determines how fast requests are processed. If you set the rate to 120r/m, that means 1 request will get processed every 0.5 seconds.

If you get 5 requests all at once, it doesn't mean that they'll all go through. They'll get queued up (up to your burst size), and get processed sequentially, 1 every 0.5 seconds.

You're getting that warning because things are going into the queue and getting delayed. It does not mean that they're getting rejected.

If you don't want things getting delayed, then use the nodelay parameter:

location "/api" {
  limit_req zone=nocachelimit burst=20 nodelay;
}

For more details on exactly how this works, see https://www.nginx.com/blog/rate-limiting-nginx/