Home » » How To Limit Download Speed Per Ip On NginX

How To Limit Download Speed Per Ip On NginX



Learn to Limit Download Speed Per IP on NginX easily. You can now throttle Speed / Bandwidth on IP Level in Nginx regardless number of connections/requests.

Limit Download Speed Per IP on NginX – Overview

 Previously our dedicated servers were on Apache. We had CBand module on Apache which was able to limit users download speed on IP level. But after Migration to NginX web server, we were in search of alternative of CBand for Nginx. Initially we couldn’t find a way to throttle speed on IP basis. Because the nginx default Limit rate couldn’t server the purpose. it only had two options. Either we could limit connections which an IP make. Or we could limit each connection speed.

Steps to Limit Download Speed Per IP in NginX

 Below is step by step tutorial to restrict Nginx download speed per IP. If you have already installed NginX then you can skip initial steps of NginX installations. 

 Step # 1 Install Pre-Requisites Before NginX Source Compilation 
You can Download All Commands from bottom button. Don’t worry if you can’t copy.  sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev   

 Step # 2 Download NginX Source and IP Speed Limiter Module    
cd /root/
 mkdir src
 cd src/
 wget http://nginx.org/download/nginx-1.4.4.tar.gz
 tar -xvzf nginx-1.4.4.tar.gz 
wget https://github.com/bigplum/Nginx-limit-traffic-rate-module/archive/master.zip

Step # 3 Compile NginX Source and Configure Module Path  
  ./configure --prefix=/opt/nginx --user=www-data --group=www-data --without-http_scgi_module --without-http_uwsgi_module --without-http_fastcgi_module --add-module=/root/src/Nginx-limit-traffic-rate-module-master --with-http_stub_status_module  --with-file-aio  

  Step # 4 Installing NginX    
make 
sudo make install

Step # 5 Edit Init File To Add NginX in Service and AutoStart in Startup 
Edit Below File and Paste the content listed below:-  vi /etc/init/nginx.conf 
# nginx 
description "nginx http daemon" 
author "Philipp Klose <me@'thisdomain'.de>" 
 start on (filesystem and net-device-up IFACE=lo) 
 stop on runlevel [!2345]
  env DAEMON=/opt/nginx/sbin/nginx
 env PID=/opt/nginx/logs/nginx.pid 
 expect fork
 respawn
 respawn limit 10 5 
#oom never 
 pre-start script 
$DAEMON -t 
 if [ $? -ne 0 ] 
   then exit $? fi 
 end script 
exec $DAEMON 
Press Escape and :wq to write changes.

Step # 6 Edit NginX Configuration File to Limit Speed Per IP
 Add below two lines in http section  limit_traffic_rate_zone rate $remote_addr 500m;
 limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
 The bold figure is the overall server speed. For example if server has 800 MB upload speed in total then edit it to 800m. Then under Server Section Add Following Line. This will limit connections Per IP. You can skip it if you don’t want to Limit connections per IP. But it is good to prevent DDOS Attack.
  limit_conn conn_limit_per_ip 56; 
Then Add Location section Inside Server Section like below example: If you want to Limit speed to 2 MB per IP then edit it as below. Or if you want to limit NginX speed to 5 MB per IP then change it to 5120K.  location /directory_example/ {  limit_traffic_rate rate 2048k;  }

Step # 7 Restarting NginX to Apply Changes 
Now Save Nginx.conf File and Restart NginX with below commands.
  service nginx stop 
service nginx start
 Let me know if you hit any issues while throttling Speed Per IP basis in Nginx.



Share this article :

Post a Comment