【问题标题】:Nginx Rate limit GET or POST requests only at a locationNginx 速率限制 GET 或 POST 请求仅在某个位置
【发布时间】:2020-10-27 20:22:25
【问题描述】:

我在 nginx 中配置了一个服务器,并使用以下代码来创建我的速率限制区域:

limit_req_zone $key zone=six_zone:10m rate=60r/m;

在我的位置,我使用一个模块来处理请求。此位置支持 GET、POST 和 DELETE 方法。我正在尝试仅对对该位置的 GET 请求进行速率限制。这是我认为可能有效的方法,但它没有。

location /api/ {
    if ($request_method = GET) {
        limit_req zone=six_zone;
    }
    reqfwder;
}

关于我如何解决这个问题的任何帮助或指示?谢谢。

【问题讨论】:

    标签: nginx nginx-location nginx-config rate-limiting


    【解决方案1】:

    希望这会有所帮助,

    在 NGINX 配置的 http 上下文中,添加以下行:

    http {
      ... # your nginx.conf here
      
      # Maps ip address to $limit variable if request is of type POST
      map $request_method $limit {
        default         "";
        POST            $binary_remote_addr;
      }
      
      # Creates 10mb zone in memory for storing binary ips
      limit_req_zone $limit zone=my_zone:10m rate=1r/s;
    }
    
    **Rate limiting for the entire NGINX process:**
    http {
        ... # your nginx.conf here
        limit_req zone=global_zone;
    }
    

    参考https://product.reverb.com/first-line-of-defense-blocking-bad-post-requests-using-nginx-rate-limiting-507f4c6eed7b

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 2011-11-29
    • 2020-07-17
    • 1970-01-01
    • 2020-10-27
    • 2018-11-05
    相关资源
    最近更新 更多