How to Limit Nginx download Speed

In my case, I used this options inside a location block… among other configurations, the main directives added are these two:

location ^~ /videos/ {

limit_rate_after 1m;
limit_rate 150k;

The fist one, limits the connection rate after the size you specify, in my case I wanted to have 1mb for the initial video buffering, and then after users reach 1mb, set the limit_rate to 150k per second, which is about 1.5MBits.

Be aware that this solution limits nginx download speed per connection, so, if one user opens multiple video files, it will be able to download 150k x the number of times he connected to the video files. If you need to set a limit to the connections, you should be able to do it with limit_zone and limit_conn directives. Example:

Inside your server block configuration:

limit_rate 128K; limit_zone one $binary_remote_addr 10m;
Inside your location block configuration:

limit_conn one 10;
In this example, it would allow 10 connections per IP with 1 Mbit each.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.