简体   繁体   English

如何在nginx中基于HTTP请求方法对流量进行速率限制?

[英]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.我想限制除 HEAD 请求之外的所有传入流量。 We have implemented a rate limit using Nginx, it is limiting all the traffics currently.我们已经使用 Nginx 实现了速率限制,它限制了当前的所有流量。 But I want to exclude the HEAD requests from the rate limit.但我想从速率限制中排除 HEAD 请求。

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

According to the limit_req_zone directive documentation :根据limit_req_zone指令文档

Requests with an empty key value are not accounted.不考虑具有空键值的请求。

So just made zone key an empty string in case of HEAD request method:因此,在HEAD请求方法的情况下,只需将区域键设为空字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM