首页 > 解决方案 > Nginx:如何正确设置速率限制?

问题描述

经过艰苦的谷歌搜索,我找不到如何使用 nginx 和 debian 限制速率的好信息。

我不确定我应该在哪里添加过滤器 nginx.conf 或默认值。我将此添加到默认值,不确定是否正确:

limit_req_zone  $binary_remote_addr  zone=one:10m   rate=1r/s;
server {
   server_name Realesta74.net;

   location / {
     limit_req zone=one burst=5;
  }

}

可以吗?以及如何测试它?

标签: nginx

解决方案


Nginx rate limiting is implemented identically regardless of Linux distribution (Debian, Ubuntu, etc). Nginx officially provides a good tutorial and explaination:

https://www.nginx.com/blog/rate-limiting-nginx/

Testing can be done with multiple-connection URL tools like Siege or Apache Benchmark Tool, and Siege this example uses 30 simultaneous connections to test rate limiting on a web page. 503 responses would show if rate limiting is configured properly:

$ siege -c 30 -r 1 --no-parser https://www.example.com/

推荐阅读