|
|
@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
<h1><center>Nginx流量限制</center></h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
作者:行癫(盗版必究)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 一:流量限制
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.简介
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
流量限制 (rate-limiting),是Nginx中一个非常实用,却经常被错误理解和错误配置的功能。我们可以用来限制用户在给定时间内HTTP请求的数量。请求,可以是一个简单网站首页的GET请求,也可以是登录表单的 POST 请求。流量限制可以用作安全目的,比如可以减慢暴力密码破解的速率。通过将传入请求的速率限制为真实用户的典型值,并标识目标URL地址(通过日志),还可以用来抵御DDOS 攻击。更常见的情况,该功能被用来保护上游应用服务器不被同时太多用户请求所压垮。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.如何限流
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Nginx的”流量限制”使用漏桶算法(leaky bucket algorithm),该算法在通讯和分组交换计算机网络中广泛使用,用以处理带宽有限时的突发情况。就好比,一个桶口在倒水,桶底在漏水的水桶。如果桶口倒水的速率大于桶底的漏水速率,桶里面的水将会溢出;同样,在请求处理方面,水代表来自客户端的请求,水桶代表根据”先进先出调度算法”(FIFO)等待被处理的请求队列,桶底漏出的水代表离开缓冲区被服务器处理的请求,桶口溢出的水代表被丢弃和不被处理的请求。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 3.基本配置
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
模块
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ngx_http_limit_req_module
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
指令
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limit_req_zone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limit_req
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
参数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limit_req_zone指令设置流量限制和共享内存区域的参数,但实际上并不限制请求速率。所以需要通过添加limit_req指令,将流量限制应用在特定的location或者server块。在上面示例中,我们对/login/请求进行流量限制。现在每个IP地址被限制为每秒只能请求10次/login/,更准确地说,在前一个请求的100毫秒内不能请求该URL。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
案例
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
upstream myweb {
|
|
|
|
|
|
|
|
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
location /login {
|
|
|
|
|
|
|
|
limit_req zone=mylimit;
|
|
|
|
|
|
|
|
proxy_pass http://myweb;
|
|
|
|
|
|
|
|
proxy_set_header Host $host:$server_port;
|
|
|
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
真实web服务器
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
location /login {
|
|
|
|
|
|
|
|
root /usr/share/nginx/html;
|
|
|
|
|
|
|
|
index index.html index.html;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
总结:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limit_req_zone指令定义了流量限制相关的参数,而limit_req指令在出现的上下文中启用流量限制
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limit_req_zone指令通常在HTTP块中定义,使其可在多个上下文中使用,它需要以下三个参数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Key - 定义应用限制的请求特性。示例中的 Nginx 变量$binary_remote_addr,保存客户端IP地址的二进制形式。这意味着,我们可以将每个不同的IP地址限制到,通过第三个参数设置的请求速率。(使用该变量是因为比字符串形式的客户端IP地址$remote_addr,占用更少的空间)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Zone - 定义用于存储每个IP地址状态以及被限制请求URL访问频率的共享内存区域。保存在内存共享区域的信息,意味着可以在Nginx的worker进程之间共享。定义分为两个部分:通过zone=keyword标识区域的名字,以及冒号后面跟区域大小。16000个IP地址的状态信息,大约需要1MB,所以示例中区域可以存储160000个IP地址
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Rate - 定义最大请求速率。在示例中,速率不能超过每秒10个请求。Nginx实际上以毫秒的粒度来跟踪请求,所以速率限制相当于每100毫秒1个请求。因为不允许”突发情况”(见下一章节),这意味着在前一个请求100毫秒内到达的请求将被拒绝。(1秒(s)=1000毫秒(ms))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 二:高级限流
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
通过将基本的“流量限制”与其他Nginx功能配合使用,我们可以实现更细粒度的流量限制
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 1.白名单
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
对任何不在白名单内的请求强制执行“流量限制”
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
http {
|
|
|
|
|
|
|
|
include /etc/nginx/mime.types;
|
|
|
|
|
|
|
|
default_type application/octet-stream;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
|
|
|
|
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
|
|
|
|
|
|
'"$http_user_agent" "$http_x_forwarded_for"';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
access_log /var/log/nginx/access.log main;
|
|
|
|
|
|
|
|
geo $limit {
|
|
|
|
|
|
|
|
default 1;
|
|
|
|
|
|
|
|
10.0.0.0/24 0;
|
|
|
|
|
|
|
|
192.168.0.0/24 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
map $limit $limit_key {
|
|
|
|
|
|
|
|
0 "";
|
|
|
|
|
|
|
|
1 $binary_remote_addr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
limit_req_zone $limit_key zone=req_zone:10m rate=5r/s;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
|
|
|
|
limit_req zone=req_zone;
|
|
|
|
|
|
|
|
root /usr/share/nginx/html;
|
|
|
|
|
|
|
|
index index.html index.hml;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
include /etc/nginx/conf.d/*.conf;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
geo块将给在白名单中的IP地址对应的$limit变量分配一个值0,给其它不在白名单中的分配一个值1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如果$limit变量的值是0,$limit_key变量将被赋值为空字符串
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
如果$limit变量的值是1,$limit_key变量将被赋值为客户端二进制形式的IP地址
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
两个指令配合使用,白名单内IP地址的$limit_key变量被赋值为空字符串,不在白名单内的被赋值为客户端的IP地址
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
当limit_req_zone后的第一个参数是空字符串时,不会应用“流量限制”,所以白名单内的IP地址(10.0.0.0/24和192.168.0.0/24 网段内)不会被限制其它所有IP地址都会被限制到每秒5个请求。limit_req指令将限制应用到/的location块,允许在配置的限制上最多超过10个数据包的突发,并且不会延迟转发。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#### 2.日志记录
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
默认情况下,Nginx会在日志中记录由于流量限制而延迟或丢弃的请求,如下所示:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
2019/02/13 04:20:00 [error] 120315#0: *32086 limiting requests, excess: 1.000 by zone "mylimit", client: 192.168.1.2, server: nginx.com, request: "GET / HTTP/1.0", host: "nginx.com"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
limiting requests - 表明日志条目记录的是被“流量限制”请求
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
excess - 每毫秒超过对应“流量限制”配置的请求数量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
zone - 定义实施“流量限制”的区域
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client - 发起请求的客户端IP地址
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server - 服务器IP地址或主机名
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request - 客户端发起的实际HTTP请求
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
host - HTTP报头中host的值
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
默认情况下,Nginx以error级别来记录被拒绝的请求,如上面示例中的[error]所示(Nginx以较低级别记录延时请求,一般是info级别)。如要更改Nginx的日志记录级别,需要使用limit_req_log_level指令
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
案例:设置日志记录中日志级别
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
|
|
|
|
|
|
|
|
upstream myweb {
|
|
|
|
|
|
|
|
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
location /login {
|
|
|
|
|
|
|
|
limit_req zone=mylimit;
|
|
|
|
|
|
|
|
limit_req_log_level warn;
|
|
|
|
|
|
|
|
proxy_pass http://myweb;
|
|
|
|
|
|
|
|
proxy_set_header Host $host:$server_port;
|
|
|
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
案例:发送到客户端的错误代码
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
一般情况下,客户端超过配置的流量限制时,Nginx响应状态码为503(Service Temporarily Unavailable)。可以使用limit_req_status指令来设置为其它状态码(例如下面的404状态码)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
|
|
|
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
|
|
|
|
|
|
|
|
upstream myweb {
|
|
|
|
|
|
|
|
server 10.0.105.196:80 weight=1 max_fails=1 fail_timeout=1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
server {
|
|
|
|
|
|
|
|
listen 80;
|
|
|
|
|
|
|
|
server_name localhost;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
location /login {
|
|
|
|
|
|
|
|
limit_req zone=mylimit;
|
|
|
|
|
|
|
|
limit_req_log_level warn;
|
|
|
|
|
|
|
|
limit_req_status 404;
|
|
|
|
|
|
|
|
proxy_pass http://myweb;
|
|
|
|
|
|
|
|
proxy_set_header Host $host:$server_port;
|
|
|
|
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|