brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx limit_rate_after lets a download start fast and then slows it down

Nginx
~/tips/tip-nginx-limit-rate-after

limit_rate_after 500k;

Throttles big downloads per connection after the first part, so small files stay quick.

One visitor pulling a 4 GB file over a fast line can fill a small upload link. nginx can cap the speed per connection, and leave the start of every file at full speed.

location /downloads/ {
    limit_rate_after 500k;
    limit_rate 200k;
}

limit_rate is in bytes per second per connection, so 200k is about 200 KB/s. limit_rate_after lets the first 500 KB of each response go out without a limit. Pages, images and small files never notice the throttle, and only large downloads get slowed. Take a 1 MB file: without a limit it arrives at once, with only limit_rate 200k it takes about four seconds, and with the 500k head start about two.

A backend can also set the rate per response with an X-Accel-Limit-Rate header, which I find handy when the app knows who is a paying user.

The limit is per connection, not per visitor. A download manager that opens eight connections gets eight times the speed. To cap a client as a whole, combine it with limit_conn, so one IP can only open a few connections. Since nginx 1.17 the values can contain variables, so you can set them through a map, for example faster for your own network.

Further reading: limit_rate and limit_rate_after

Going deeper: 🛜 Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link