在Nginx中,可以使用ngx_http_limit_req_module
模块来限制访问速度。以下是如何在Ubuntu上配置Nginx以限制访问速度的步骤:
sudo apt update
sudo apt install nginx
/etc/nginx/conf.d/rate-limit.conf
:sudo nano /etc/nginx/conf.d/rate-limit.conf
http {
# 限制每个客户端的请求速率
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
server {
listen 80;
server_name example.com;
location / {
# 应用限制
limit_req zone=mylimit burst=5 nodelay;
root /var/www/html;
index index.html index.htm;
}
}
}
在这个例子中,我们限制了每个客户端的请求速率为每秒1个请求,允许短时间内的突发请求达到5个。nodelay
参数表示不对请求进行延迟处理,使得限制更加平滑。
保存并关闭配置文件。
检查Nginx配置文件的语法是否正确:
sudo nginx -t
sudo systemctl reload nginx
现在,Nginx已经配置好了访问速度限制。根据需要,可以根据实际情况调整rate
和burst
参数。