Nginx入门基础-访问配置
Nginx访问限制
Nginx访问限制:ngx_http_limit_req_module,ngx_http_limit_conn_module
启动访问限制
ngx_http_limit_req_module
vim /etc/nginx/nginx.confhttp {limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; #定义限制请求 二进制地址 限制策略的名称 占用10M空间 允许每秒1次请求server {location / {root /usr/share/nginx/html;index index.html index.htm;limit_req zone=req_zone; #引用 限制策略的名称#limit_req zone=req_zone burst=5; #引用限制,但是令牌桶有5个。有延迟。速度慢#limit_req zone=req_zone burst=5 nodelay; #引用限制,但是令牌桶有5个。无延迟。速度快}}
}测试
yum install -y httpd-tools
ab -n 100 -c 10 http://ts.com/结果
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient).....doneServer Software: nginx/1.12.1
Server Hostname: tianyun.me
Server Port: 80Document Path: /
Document Length: 671 bytesConcurrency 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观察错误日志
tail -f /var/log/nginx/error.log
2017/10/08 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: "ts.com"limiting requests #由于限制请求导致。
ngx_http_limit_conn_module
目的:通过IP地址,限制链接(TCP)。但是实验环境无法测试
#启动连接频率限制vim /etc/nginx/nginx.confhttp {limit_conn_zone $binary_remote_addr zone=conn_zone:10m; #limit_conn_zone:全局定义限制对象(IP),存储区限制空间(10M)字节
}
server {location / {...limit_conn conn_zone 1;}
} #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连接
结果
yum install -y httpd-toolsab -n 100 -c 10 http://服务器IP地址/This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Benchmarking localhost (be patient).....doneServer Software: nginx/1.12.1
Server Hostname: tianyun.me
Server Port: 80Document 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) 每秒钟处理多少个请求。Nginx访问控制
基于主机(ip)module ngx_http_access_module
Directives
allow 允许某些主机
deny 拒绝某些主机
Syntax:Syntax: allow address | CIDR | unix: | all;Context: http, server, location, limit_except基于用户(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 限制主机访问
vim /etc/nginx/conf.d/default.confserver {allow 10.18.45.65;allow 10.18.45.181;deny all;
}2 测试 服务器无法访问#基于用户
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.confserver { #找到server{字段,在下一行插入认证字段。auth_basic "nginx access test!"; #提示消息auth_basic_user_file /etc/nginx/conf.d/passwd; #引用认证文件...
}重启访问
