[ nginx ] 帶寬下載限速

  nginx上了一個APP提供給用戶下載,考慮到帶寬佔用的問題,決定在nginx上作下載限速處理。html

操做系統:Centos6.7 X64nginx

nginx版本:nginx/1.11.3瀏覽器

根據官方文檔:測試

 

對nginx.conf進行配置:網站

http {
  ...
  limit_conn_zone $binary_remote_addr zone=addr:10m;   # 添加該行
  ...
include vhost/*.conf; }


由於我這裏是APP下載業務是定義的一個nginx虛擬主機spa

  
[root@localhost vhost]# pwd
/usr/local/nginx/conf/vhost

1 server { 2 listen *:8080; 3 server_name localhost; 4 location / { 5 root /usr/local/test; 6 index index.html; 7 limit_conn addr 1; # 每一個客戶端只容許一個線程。 8 limit_rate 100k;  # 每一個線程最大下載速度100k 9 } 10 11 }
 
每一個客戶端最終的下載速度 = limit_conn * limit_rate 我這裏很明顯是100kb/s

 

啓動服務,並進行測試:操作系統

在網站根目錄寫入一個大文件並嘗試下載:線程

[root@localhost test]# dd if=/dev/zero of=test.txt bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 2.99695 s, 350 MB/s

 

本地直接使用wget進行測試: 最大速度100kb/scode

 

客戶端使用瀏覽器進行測試:server

 

 能夠看見,nginx限速成功。我一個是本地測試,一個是局域網內部測試,固然公網測試就更加明顯了。