How to Limit NGINX Download Speed


limit nginx download speed

If you do not limit the download speeds on your website, then it can slow down your website performance and increase server load. This is because each user that downloads a big file ends up consuming more data bandwidth than an average website visitor who is just browsing your website. So let us look at how to limit NGINX download speed for your website.

 

How to Limit NGINX Download Speed

Here are the steps to limit NGINX download speed. We will using a few NGINX server directives for this purpose.

 

1. Limit Download Speed

Open your nginx config file in a text editor


$ sudo vim /etc/nginx/nginx.conf

 

Create a location block for the directory(or file) for which you want to limit download speed. In our example, we will limit download speed for /videos/ directory. If the location block already exists, you can simply add the limit_rate_after and limit_rate as shown below.


location ^~ /videos/ {
 ...
 limit_rate_after 1m;
 limit_rate 150k;
 ...
}
  • limit_rate_after – limit download speed after the specified file size has been downloaded
  • limit_rate – max download rate limit

In our example, the rate limit will be applied only after 1Mb of data is downloaded by the client. The limited rate will be 150kbps. You can change the values as per your requirement.

 

2. Limit Connections per User

The above solution only limits download rates per connection. So if a user downloads the same file multiple times simultaneously, then the download rate will 150kbps x no. of connections.

If you want to also limit number of connections, add the following code in the server block of your nginx config file


limit_rate 128K; limit_zone one $binary_remote_addr 10m;

and add the following code in your location block


limit_conn one 10;

The above code will limit no. of connections to 10 connections per IP 1Mb each.

 

That’s it! You can now limit NGINX download rate for your website.

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!