Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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;
}
...
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading