配置limit_conn_zone来限制并发连接数以及下载带宽
配置nginx限制流量和连接数;
[[email protected] ~]# rpm -q nginx #我这里使用1.8的nginx版本,1.2以后应该都是支持的;
nginx-1.8.0-1.el6.ngx.x86_64
[[email protected] ~]# cd /etc/nginx/
[[email protected] nginx]# vim nginx.conf #在nginx.conf里的http{}里加上相关代码:
http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; #配置区域名称为perip,需要写在http里面; limit_conn_zone $binary_remote_addr zone=perip:10m; ....后面省略 }
[[email protected] nginx]# cd conf.d/
[[email protected] conf.d]# vim default.conf #在相关需要限制的server{}里加上相关代码:
server { listen 80; server_name localhost; #charset koi8-r; access_log /var/log/nginx/log/host.access.log main; limit_conn perip 10; #这里调用上面的perip,需要写在server里面; limit_rate 100k; #限制每个连接的带宽,可以单独写这条,与连接数无关; ...........后面省略 }
补充说明下参数:
- $binary_remote_addr是限制同一客户端ip地址;
- $server_name是限制同一server最大并发数;
- limit_conn为限制并发连接数;
- limit_rate为限制下载速度;
测试1000个并发:
[[email protected] nginx]# ab -kc 1000 -n 1000 http://192.168.10.65/index.php
Concurrency Level: 1000 #并发级别 Time taken for tests: 1.220 seconds #测试时间 Complete requests: 1000 #完成请求 Failed requests: 947 #失败的请求 (Connect: 0, Receive: 0, Length: 947, Exceptions: 0) Write errors: 0 Non-2xx responses: 1000 Keep-Alive requests: 947 Total transferred: 714754 bytes HTML transferred: 518715 bytes Requests per second: 819.75 [#/sec] (mean) Time per request: 1219.889 [ms] (mean) Time per request: 1.220 [ms] (mean, across all concurrent requests) Transfer rate: 572.18 [Kbytes/sec] received
流量测试暂时不知道要怎么测试;
时间: 2024-10-15 08:33:48