0%

nginx之请求限流限速问题

单位时间按照IP地址限速

工作当中经常需要按照IP地址限速,加白名单或者黑名单,这里面就会用到map来设置。看下边例子展示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
http {  
geo $white_ip {
ranges;
default 0;
127.0.0.1-127.0.0.1 1;

36.110.16.242-36.110.16.242 1

}


map $white_ip $white_ip_address {
0 $binary_remote_addr;
1 "";
}

limit_req_zone $white_ip_address zone=:10m rate=20r/s;

....

}

解释下

上述通过geo模块 设定了白名单

ngx_http_geo_module 模块可以用来创建变量,其值依赖于客户端IP地址。
语法: geo [$address] $variable { … } 设置在http 模块中
[$address] 可以为空,使用默认变量也就是$remote_addr 其实例子中 geo $white_ip {} 就相当于geo $remote_addr $white_ip {}
$white_ip 命名为white_ip
ranges 使用以地址段的形式定义地址,这个参数必须放在首位。为了加速装载地址库,地址应按升序定义。
default 设置默认值,如果客户端地址不能匹配任意一个定义的地址,nginx将使用此值。

注:如果36.110.16.242 这个IP地址访问本站, white_ip 这个变量值就是 1,否则就是0

通过map模块 对$white_ip进行映射

ngx_http_map_module 模块可以创建变量,这些变量的值与另外的变量值相关联(上文的$white_ip)。允许分类或者同时映射多个值到多个不同值并储存到一个变量中,map指令用来创建变量,但是仅在变量被接受的时候执行视图映射操作,对于处理没有引用变量的请求时,这个模块并没有性能上的缺失。

语法: map $var1 $var2 { … }

上文map指令是将$white_ip值为0的,也就是受限制的IP,映射为客户端IP。将$white_ip值为1的,映射为空的字符串。
limit_conn_zonelimit_req_zone指令对于键为空值的将会被忽略,从而实现对于列出来的IP不做限制。

limit_req_zone 真正操作限速
ngx_http_limit_req_module 模块

下载限速

1
2
3
4
5
6
7
8
9
10
11
location /file { 
limit_rate 128k;
# 限制下载速度128K/s
}

# 如果想设置用户下载文件的前10m大小时不限速,大于10m后再以128kb/s限速可以增加以下配内容,修改nginx.conf文件

location /download {
limit_rate_after 10m;
limit_rate 128k;
}

屏蔽请求

1.单一url屏蔽

2.动态url屏蔽 如http://yourvhost/abc123.html bac456.html

3.屏蔽referer

4.屏蔽http_host

5.屏蔽来源IP

6.屏蔽文件类型

7.屏蔽目录

8.屏蔽一个文件

9.多条件叠加屏蔽 例子为 屏蔽 url为这个 and 浏览器为windows and host为这个的请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

1.anti a request this url is single url ex : http://yourvhost/xxxx.html

location / {

if ($request_uri ~* ^/index.html$){

return 403;
}
}

2.anti a dynamic url ex : http://yourvhost/abc123.html bac456.html

location / {

if ($request_uri ~* "^/test/\w{1,3}\d{1,3}}\.html$"){

return 403;

}
}
3.anti a referer

location / {

if ($http_referer ~* ^http://test.360.cn/index.html$){

return 403;

}
}
4.anti a http_host

location / {

if ($http_host ~* ^test.360.cn$){
return 403;
}
}
5.anti source IP

location / {

deny 192.168.100.42;
deny 192.168.0.0/16;
deny 192.168.100.0/24;

}

6.anti file type

location ~* \.(gif|jpg|jpeg|html)$

{
deny all;
}

7.anti directory

location ~ ^/(test|tset)/
{
deny all;
}

8.anti a file

location ~ /test/test.html {
deny all;
}

9.Multiple conditions anti ex : url is http://yourvhost/test/aaa111.html AND browser is Windows AND http_host is test.360.cn

if ($request_uri ~ "^/test/\w{1,3}\d{1,3}}\.html$") {

set $iftmp Y;
}

if ($http_user_agent ~ ".*Windows NT 6.1.*") {

set $iftmp "${iftmp}Y";

}
if ($http_host ~* ^test.360.cn$) {

set $iftmp "${iftmp}Y";
}
if ($iftmp = YYY) { return 403;}
}