HTTP协议已的连接与请求
- Http请求简历在一次TCP连接基础上
- 一次TCP请求至少产生一次HTTP请求
nginx请求限制配置
连接频率限制 - limit_conn_module
Syntax:limit_conn_zone key zone=name:size;
Default: --
Context:http
Syntax:limit_conn number;
Default: --
Context:http,server,location
这个模块的目的主要是对连接进行限制,如果是这样的话,那么我们就需要对连接的状态进行进行存储。那么是用什么来进行存储呢?存储肯定是需要空间的。这个limit_conn_zone就是开辟了这样的一个空间。这个空间里,我们需要对那个作为key要进行说明,比如说,以客户端ip作为Key。那么这样的话,就以http_addr这个变量作为Key。如果要以别的内置变量作为key来作为key来进行配置的时候,那么同样,可以写到key配置的这一项中。那么后面的zone=name:size,就是限制的空间,name指的是空间的名字,size表示的是空间的大小。在真正实现限制的时候就会调用这个空间。在limit_conn zone number中就可以调用这个空间。在limit_conn zone number中,是要结合先定义好的zone 才能使用limit_conn.这个zone指的是我们需要调用的zone的name.number指的是并发的限制个数。
请求频率限制 - limit_req_module
Syntax:limit_conn_zone key zone=name:size rate=rate;
Default: --
Context:http
Syntax:limit_req zone=name[burst=number][nodelay];
Default: --
Context:http,server,location
Rate:表示限制请求的速率是多大,一般来说,是以秒为单位的。
实战
limit_conn_zone $binary_remote_addr zone=conn_zone:1m;
# 同一个客户端ip过来的,一秒钟发起一个请求
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
server {
location / {
root /usr/share/nginx/html;
limit_conn conn_zone 1;
#limit_req配置中, burst=3代表遗留3个相应到下一秒执行. nodelay代表直接返回503.通常用在延时访问.
# limit_req zone=req_zone burst=3 nodelay;
# limit_req zone=req_zone burst=3
limit_req zone=req_zone;
}
}
ab压力测试
ab -n 10 -c 2 192.168.2.16
这个 -n 10 -c 2 ,是一共请求10次,每次并发请求数为2,总数是10不是20.
[root@colud ~]# ab -n 10 -c 10 http://127.0.0.1/index.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 123.206.9.108 (be patient).....done
Server Software: nginx/1.14.2
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /index.html
Document Length: 537 bytes
Concurrency Level: 10
Time taken for tests: 0.298 seconds
Complete requests: 10
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Non-2xx responses: 9
Total transferred: 7424 bytes
HTML transferred: 5445 bytes
Requests per second: 33.55 [#/sec] (mean)
Time per request: 298.035 [ms] (mean)
Time per request: 29.803 [ms] (mean, across all concurrent requests)
Transfer rate: 24.33 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 50 76 38.1 61 148
Processing: 50 77 38.9 61 150
Waiting: 50 77 38.9 61 150
Total: 100 152 76.9 121 298
Percentage of the requests served within a certain time (ms)
50% 121
66% 123
75% 125
80% 297
90% 298
95% 298
98% 298
99% 298
100% 298 (longest request)
nginx 错误日志
2019/04/22 21:15:32 [error] 17622#17622: *8002 limiting requests, excess: 0.822 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:32 [error] 17622#17622: *8425 limiting requests, excess: 0.312 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8631 limiting requests, excess: 1.000 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8632 limiting requests, excess: 0.990 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8633 limiting requests, excess: 0.990 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8634 limiting requests, excess: 0.989 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8635 limiting requests, excess: 0.986 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8636 limiting requests, excess: 0.888 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8637 limiting requests, excess: 0.865 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8638 limiting requests, excess: 0.805 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8639 limiting requests, excess: 0.805 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8640 limiting requests, excess: 0.803 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8641 limiting requests, excess: 0.792 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8642 limiting requests, excess: 0.696 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8643 limiting requests, excess: 0.684 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"
2019/04/22 21:15:41 [error] 17622#17622: *8644 limiting requests, excess: 0.673 by zone "req_zone", client: 192.168.2.2, server: localhost, request: "GET /index.html HTTP/1.0", host: "123.206.9.108"