一.访问限制
1.ngx_http_limit_req_module
目的:限制访问主题(用户,进程,服务等)对访问客体(文件,系统等)的访问权限,从而使计算机系统在合法范围内使用
启动请求频率限制
1)测试未限制情况下的访问
yum install -y httpd-tools
ab -n 100 -c 10 http://a.com/
2) 启动限制
vim /etc/nginx/nginx.conf
定义
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
限制请求 二进制地址 限制策略的名称 占用10M空间 允许每秒1次请求
引用
limit_req zone=req_zone;
引用 限制策略的名称
配置
http {
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; 定义
server {
location / {
root /usr/share/nginx/html;
index index.html index.htm;
limit_req zone=req_zone; 引用
#limit_req zone=req_zone burst=5;
#limit_req zone=req_zone burst=5 nodelay;
}
}
}
引用限制
引用限制,但是令牌桶有5个。有延迟。速度慢
引用限制,但是令牌桶有5个。无延迟。速度快
burst=5 表示最大延迟请求数量不大于5。 如果太过多的请求被限制延迟是不需要的 ,这时需要使用nodelay参数,服务器会立刻返回503状态码。
3)重启服务,并测试
yum install -y httpd-tools
ab -n 100 -c 10 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient).....done
Server Software: nginx/1.18.1
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /
Document Length: 671 bytes
Concurrency Level: 10
Time taken for tests: 0.006 seconds
Complete requests: 100
Failed requests: 99 失败的请求
(Connect: 0, Receive: 0, Length: 99, Exceptions: 0)
Write errors: 0
Non-2xx responses: 99 有问题的相应。
Total transferred: 73273 bytes
HTML transferred: 53834 bytes
Requests per second: 16131.63 [#/sec] (mean)
Time per request: 0.620 [ms] (mean)
Time per request: 0.062 [ms] (mean, across all concurrent requests)
Transfer rate: 11543.10 [Kbytes/sec] received
4)观察错误日志
tail -f /var/log/nginx/error.log
2020/09/18 01:05:08 [error] 23287#23287: *720 limiting requests, excess: 5.109 by zone “req_zone”,
client: 27.216.240.201, server: localhost, request: “GET / HTTP/1.0”, host: “127.0.0.1”
2.ngx_http_limit_conn_module
目的:通过IP地址,限制链接(TCP)。但是实验环境无法测试
启动连接频率限制
启动连接频率限制
vim /etc/nginx/nginx.conf
http {
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
}
server {
location / {
...
limit_conn conn_zone 1;
}
}
limit_conn_zone:全局定义限制对象(IP),存储区限制空间(10M)字节
limit_conn:该指令指定每个给定键值的最大同时连接数,当超过这个数字时返回503(Service )错误。如(同一IP同一时间只允许有2个连接):
客户端的IP地址作为键。注意,这里使用的是 binary_remote_addr 变量,而不是 remote_addr 变量。
remote_addr变量的长度为7字节到15字节,而存储状态在32位平台中占用32字节或64字节,在64位平台中占用64字节。
binary_remote_addr变量的长度是固定的4字节,存储状态在32位平台中占用32字节或64字节,在64位平台中占用64字节。
1M共享空间可以保存3.2万个32位的状态,1.6万个64位的状态。
如果共享内存空间被耗尽,服务器将会对后续所有的请求返回 503 (Service Temporarily Unavailable) 错误。
limit_conn_zone $binary_remote_addr zone=conn_zone:10m模块开启对单个ip、单个会话同时存在的连接数的限制。这里定义一个记录区conn_zone,conn_zone的总容量是10m,该记录区针对于变量 $binary_remote_add生效,这里是针对单个IP生效。该模块只是一个定义,配置在http配置段,需要配合limit_conn指令使用才生效, limit_conn conn_zone 1表示该location段使用conn_zone定义的 limit_conn_zone ,对单个IP限制同时存在一个连接。
单个IP,同时只允许有一个tcp连接
测试 ab -n 100 -c 10 http://服务器IP地址/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient)…done
Server Software: nginx/1.18.1
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /
Document Length: 671 bytes 文档长度
Concurrency Level: 10 当前并发数
Time taken for tests: 0.006 seconds 消耗总时间
Complete requests: 100 完成请求数
Failed requests: 0 失败请求数
Write errors: 0
Total transferred: 90400 bytes 总的传输大小
HTML transferred: 67100 bytes http传输大小
Requests per second: 15873.02 [#/sec] (mean) 每秒钟处理多少个请求。
二.访问控制
1.基于主机(ip)
module
ngx_http_access_module
Directives
allow
允许某些主机
deny
拒绝某些主机
Syntax:
Syntax: allow address | CIDR | unix: | all;
Context: http, server, location, limit_except
启用控制
1 限制主机访问
vim /etc/nginx/conf.d/default.conf
server {
allow 10.8.162.89;
allow 10.8.162.81;
deny all;
}
2 测试
服务器无法访问
2.基于用户(username&password)
module
ngx_http_auth_basic_module
Syntax:
方法一
Syntax: auth_basic string | off;
Context: http, server, location, limit_except
方法二
Syntax: auth_basic_user_file file;
Context: http, server, location, limit_except
启用控制
1. 建立认证文件
yum install -y httpd-tools
生成秘钥的工具是由apache提供
htpasswd -cm /etc/nginx/conf.d/passwd user10
会话密码
htpasswd -m /etc/nginx/conf.d/passwd user20
会话密码
cat /etc/nginx/conf.d/passwd
观察口令文件是否生成。已生成
user10:$apr1$UE/tLtDM$nVm686kAMYb/ArqQDUi8U/
user20:$apr1$bmn0E/gK$enkXKb2V5uFvUy9wdIHlP.
2. 启动认证
vim /etc/nginx/conf.d/default.conf
server {
找到server{字段,在下一行插入认证字段。
auth_basic "nginx access test!";
auth_basic_user_file /etc/nginx/conf.d/passwd;
提示消息
引用认证文件
...
}
3.重启并验证