Home > Mobile >  How to rate limit the traffic based on HTTP request method in nginx?
How to rate limit the traffic based on HTTP request method in nginx?

Time:06-18

I would like to limit all the incoming traffic except for HEAD requests. We have implemented a rate limit using Nginx, it is limiting all the traffics currently. But I want to exclude the HEAD requests from the rate limit.

Here is the code snippet used for the rate limit

http {
...
limit_req_zone $binary_remote_addr zone=ratelimit:50m rate=200r/s;
limit_req_status 429 
...
...
server {
limit_req zone=ratelimit  burst=400 nodelay;
}
...
}

CodePudding user response:

According to the limit_req_zone directive documentation:

Requests with an empty key value are not accounted.

So just made zone key an empty string in case of HEAD request method:

http {
...
map $request_method $ratelimit_key {
    HEAD     '';
    default  $binary_remote_addr;
}
limit_req_zone $ratelimit_key zone=ratelimit:50m rate=200r/s;
limit_req_status 429;
...
  • Related