Linux企业运维--nginx限流与配置管理

一、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.conf

43     #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.conf

 44         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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值