Tips / Nginx

How to Limit Nginx download Speed

Today I was searching for a way to limit nginx speed on a big download dedicated server that serves tons of mp4 video files per second. The problem was simple, each user with big bandwidth connections were able to consume a lot of bandwidth, more than the average users… So, the fix for this was kinda simple, I used the limit_rate and limit_rate_after directives.

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.

More information:


Popular search terms:

  • nginx bandwidth limit
  • nginx speed limit
  • nginx limit bandwidth
  • https://www scalescale com/tips/nginx/how-to-limit-nginx-download-speed/

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • MrOwen

    Great write up, super helpful!

    However, it seems as of 1.1.8, the limit_zone is deprecated. See here: Should probably update the article to use the limit_conn_zone directive instead!

  • Simon Schick

    Thanks for the summary. I just wanted to add information your calculation you had after the first code-example:

    The unit for limit_rate is bytes per second (source).

    Here’s my calculation:

    You defined 150k for limit_rate, what now is 150 kilobytes per second. In megabits per second (sparing out all the calculation details here), this is aproximately 1.17 Mb, what is – yes – comparable to what you get using a 1.5 Mbit DSL connection.

    That just as a sidenode. Thanks for the article! Helped me a lot.

  • ary

    lots of thanks for this very helpful tutorial. This helps me much.

  • karatedog

    The limit_rate_after directive has no equivalent X-Accel header (while limit_rate has), is it possible to use a custom server response header value as a limit_rate_after value?

  • please update the article

  • Chris Breuer

    I would love to see an updated article as well!