nginx限制文件访问速率

需求: 一个文件下载功能需要限制文件同时访问的并发数和当个连接的访问速率;

配置:

在http context内增加:

limit_conn_zone $binary_remote_addr zone=limitbyaddr:10m;
limit_conn_status 429;

在location内增加:

limit_conn limitbyaddr 40;
limit_rate 1m;

用ab测试生效; ab -n 50 -c 41 http://*******zip

Server Software: nginx/1.12.0
Server Hostname: *****
Server Port: 8080

Document Path: *******.zip
Document Length: 185 bytes

Concurrency Level: 41
Time taken for tests: 454.580 seconds
Complete requests: 50
Failed requests: 40
(Connect: 0, Receive: 0, Length: 40, Exceptions: 0)
Write errors: 0
Non-2xx responses: 10
Total transferred: 19072201700 bytes
HTML transferred: 19072187090 bytes
Requests per second: 0.11 [#/sec] (mean)
Time per request: 372755.829 [ms] (mean)
Time per request: 9091.606 [ms] (mean, across all concurrent requests)
Transfer rate: 40972.29 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 21 141.4 1 1001
Processing: 1 363336 183511.5 454150 454577
Waiting: 1 7 28.3 3 203
Total: 2 363357 183471.0 454151 454578

Percentage of the requests served within a certain time (ms)
50% 454151
66% 454163
75% 454171
80% 454189
90% 454224
95% 454311
98% 454578
99% 454578
100% 454578 (longest request)

参考资料:

https://www.nginx.com/resources/library/complete-nginx-cookbook/?utm_source=nginxorg&utm_medium=homepagebanner&utm_campaign=complete_cookbook&_ga=2.155941947.1811327257.1521102376-2057340437.1501655649

原文地址:https://www.cnblogs.com/bluecarrife/p/8575076.html