How to Throttle Large Image Downloads in Nginx to Preserve Bandwidth
When a website suffers from saturated outbound bandwidth due to large image requests, you can quickly mitigate the issue by configuring Nginx to limit the transfer rate of images exceeding 100 KB, ensuring normal traffic remains responsive.
Scenario
A few days ago the site experienced slow access because the outbound bandwidth was constantly saturated. Server logs showed many requests for images around 10 MB, which consumed most of the bandwidth and severely impacted other requests. Normally only scaled‑down images under 100 KB should be served, but fixing the code would take too long, so an urgent solution was needed.
Configuration
The Nginx configuration for image requests was modified to limit the download speed of large images. The idea is to allow normal small images to be served without restriction, while images larger than 100 KB are throttled to a maximum of 100 KB/s.
location ~ .*\\.(gif|jpg|jpeg|png|bmp)$ {
expires 30d;
limit_rate_after 100k;
limit_rate 100k;
}The two directives mean that once an image has transferred 100 KB, Nginx will limit the remaining transfer rate to 100 KB/s.
Testing
The test server had a 1 M bandwidth limit. A 3.3 M image was used for the test. Before throttling, the download speed stayed around 130 KB/s, fully occupying the 1 M link. After applying the limit, the speed stabilized at about 100 KB/s, confirming that the throttling worked.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
