一、nginx限流
https://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-proxied-http/
1.最大连接数限制
修改配置文件,nginx中通过limit_comm_zone 和limit_req_zone两个组建来对客户端访问目录和文件的访问屏率和次数进行限制
limit_conn_zone $binary_remote_addr zone=addr:10m; ###定义两个参数,生成一个大小为10M,名字为addr的内存区域,连接数为第二个参数。编写指令模块。
[root@server1 ~]# cd /usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf43 #gzip on;
44 limit_conn_zone $binary_remote_addr zone=addr:10m;58 location /download/{
59 limit_conn addr 1; ##并发连接1个60 }
[root@server1 conf]# nginx -t
[root@server1 conf]# nginx -s reload
[root@server1 conf]# cd ..
[root@server1 nginx]# pwd
/usr/local/nginx
[root@server1 nginx]# cd html/
[root@server1 html]# ls
50x.html index.html index.php test.php
[root@server1 html]# mkdir download
[root@server1 html]# cd download/
[root@server1 download]# mv /root/vim.jpg .
[root@server1 download]# ls
vim.jpg
[root@server1 download]# du -h vim.jpg
256K vim.jpg
在客户端:
[root@westos_student73 Desktop]# curl -I http://172.25.10.1/download/vim.jpg
[root@westos_student73 Desktop]# ab -c1 -n10 http://172.25.10.1/download/vim.jpg
##并发1次,请求10次
[root@server1 ~]# cd /usr/local/nginx
[root@server1 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@server1 nginx]# cd logs/
[root@server1 logs]# ls
access.log error.log nginx.pid nginx.pid.oldbin
[root@server1 logs]# cat access.log
ab -c10 -n10 http://172.25.10.1/download/vim.jpg ##进行压力测试,出错了
2. 限制单位时间内的请求数目
[root@server1 logs]# cd /usr/local/nginx/conf/
[root@server1 conf]# vim nginx.conf44 limit_conn_zone $binary_remote_addr zone=addr:10m;
45 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;60 limit_conn addr 1;
61 limit_req zone=one;
62 }[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
在客户端(真机中)进行压力测试:
[root@westos_student73 Desktop]# ab -c1 -n10 http://172.25.10.1/download/vim.jpg