3

While limiting the number of requests from an IP, I am using

limit_req_zone  $binary_remote_addr zone=one:10m rate=1r/m;

I can specify the rate in r/m (requests per minute) or r/s (requests per second). Is there a way to specify the rate in requests/hour ?

For example, I want the rate to be 75 requests/hour. So it will be 1.25r/m, but r/m has to be an integer. So, even that way it doesn't work.

Please help me out.

2 Answers2

5

According to the documentation you can specify in requests per second or requests per minute, not requests per hour, so no it's not possible.

The rate is specified in requests per second (r/s). If a rate of
less than one request per second is desired, it is specified in
request per minute (r/m). For example, half-request per second
is 30r/m.

I suggest you round up or round down, according to your use case. 1r/s is 60 requests per minute, 2r/s is 120 requests per minute.

Tim
  • 30,383
  • 6
  • 47
  • 77
5

At some point, I needed the same function to define rate limits per hour, day, week, etc.

I was itchy to build a clone of ngx_http_limit_req_module first, but then just integrated the necessary changes to nginx-mod.

So this works with nginx-mod:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/h; # 1 request per hour
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/d; # 1 request per day
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/w; # 1 request per week
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/M; # 1 request per month
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/Y; # 1 request per year
Danila Vershinin
  • 4,738
  • 3
  • 16
  • 21