|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Setting up nginx Bandwidth Limiting - Complete limit_rate Guide" |
| 4 | +date: 2023-09-10 15:30:00 +0900 |
| 5 | +categories: [Development, Tutorial] |
| 6 | +tags: [nginx, server, bandwidth, limit_rate, optimization, devops] |
| 7 | +author: "Kevin Park" |
| 8 | +excerpt: "Complete guide on how to effectively limit bandwidth using limit_rate and limit_rate_after directives in nginx with practical testing examples" |
| 9 | +lang: en |
| 10 | +--- |
| 11 | + |
| 12 | +# Setting up nginx Bandwidth Limiting |
| 13 | + |
| 14 | +## 🎯 Summary |
| 15 | + |
| 16 | +The simplest way to limit bandwidth in nginx is by using the `limit_rate` directive. Here are ready-to-use configuration examples. |
| 17 | + |
| 18 | +### Immediately Applicable Configuration |
| 19 | + |
| 20 | +```nginx |
| 21 | +# /etc/nginx/nginx.conf or site-specific configuration file |
| 22 | +server { |
| 23 | + listen 80; |
| 24 | + server_name example.com; |
| 25 | + |
| 26 | + location / { |
| 27 | + # Limit speed to 200KB/s after 500MB download |
| 28 | + limit_rate_after 500M; |
| 29 | + limit_rate 200k; |
| 30 | + |
| 31 | + # File serving configuration |
| 32 | + root /var/www/html; |
| 33 | + index index.html; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### File Type-Specific Bandwidth Limiting |
| 39 | + |
| 40 | +```nginx |
| 41 | +# Video file bandwidth limiting |
| 42 | +location ~* \.(mp4|avi|mkv)$ { |
| 43 | + limit_rate_after 10M; |
| 44 | + limit_rate 500k; |
| 45 | +} |
| 46 | +
|
| 47 | +# Image file bandwidth limiting |
| 48 | +location ~* \.(jpg|jpeg|png|gif)$ { |
| 49 | + limit_rate_after 1M; |
| 50 | + limit_rate 100k; |
| 51 | +} |
| 52 | +
|
| 53 | +# General file bandwidth limiting |
| 54 | +location / { |
| 55 | + limit_rate_after 500M; |
| 56 | + limit_rate 200k; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Configuration Application Commands |
| 61 | + |
| 62 | +```bash |
| 63 | +# Check configuration file syntax |
| 64 | +sudo nginx -t |
| 65 | + |
| 66 | +# Restart nginx |
| 67 | +sudo systemctl restart nginx |
| 68 | + |
| 69 | +# Or reload configuration |
| 70 | +sudo nginx -s reload |
| 71 | +``` |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## 📚 Detailed Explanation |
| 76 | + |
| 77 | +### Background and Necessity |
| 78 | + |
| 79 | +Bandwidth limiting in nginx is essential for server resource management and user experience optimization. Especially when serving large files, it prevents server overload caused by unlimited bandwidth usage. |
| 80 | + |
| 81 | +### Technical Details |
| 82 | + |
| 83 | +#### Detailed Explanation of limit_rate Directives |
| 84 | + |
| 85 | +- **`limit_rate`**: Limits the response transmission speed to clients |
| 86 | +- **`limit_rate_after`**: Applies speed limiting after transmitting the specified amount |
| 87 | +- **Units**: `k` (kilobytes), `m` (megabytes), `g` (gigabytes) |
| 88 | + |
| 89 | +#### Dynamic Bandwidth Limiting |
| 90 | + |
| 91 | +```nginx |
| 92 | +# Dynamic limiting using variables |
| 93 | +map $request_uri $rate_limit { |
| 94 | + ~*\.(mp4|avi)$ 500k; |
| 95 | + ~*\.(jpg|png)$ 100k; |
| 96 | + default 200k; |
| 97 | +} |
| 98 | +
|
| 99 | +server { |
| 100 | + location / { |
| 101 | + limit_rate $rate_limit; |
| 102 | + limit_rate_after 1M; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +#### Per-User Bandwidth Limiting |
| 108 | + |
| 109 | +```nginx |
| 110 | +# IP-based limiting |
| 111 | +geo $limit_rate_ip { |
| 112 | + default 100k; |
| 113 | + 192.168.1.0/24 500k; # Internal network gets faster speed |
| 114 | + 10.0.0.0/8 1m; # VPN users get faster speed |
| 115 | +} |
| 116 | +
|
| 117 | +server { |
| 118 | + location / { |
| 119 | + limit_rate $limit_rate_ip; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Real-World Use Cases |
| 125 | + |
| 126 | +#### 1. nginx Server Acting as CDN |
| 127 | + |
| 128 | +```nginx |
| 129 | +server { |
| 130 | + listen 80; |
| 131 | + server_name cdn.example.com; |
| 132 | + |
| 133 | + # Static file serving |
| 134 | + location /static/ { |
| 135 | + root /var/www; |
| 136 | + |
| 137 | + # Large files transmitted slowly |
| 138 | + location ~* \.(zip|tar|gz)$ { |
| 139 | + limit_rate_after 10M; |
| 140 | + limit_rate 1m; |
| 141 | + } |
| 142 | + |
| 143 | + # Media file streaming optimization |
| 144 | + location ~* \.(mp4|mp3|flv)$ { |
| 145 | + limit_rate_after 2M; |
| 146 | + limit_rate 500k; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +#### 2. API Server Bandwidth Limiting |
| 153 | + |
| 154 | +```nginx |
| 155 | +server { |
| 156 | + listen 80; |
| 157 | + server_name api.example.com; |
| 158 | + |
| 159 | + # API response size limiting |
| 160 | + location /api/ { |
| 161 | + proxy_pass http://backend; |
| 162 | + |
| 163 | + # Large data response limiting |
| 164 | + limit_rate_after 5M; |
| 165 | + limit_rate 2m; |
| 166 | + |
| 167 | + # Proxy settings |
| 168 | + proxy_set_header Host $host; |
| 169 | + proxy_set_header X-Real-IP $remote_addr; |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Bandwidth Limiting Testing Methods |
| 175 | + |
| 176 | +#### 1. Testing with curl |
| 177 | + |
| 178 | +```bash |
| 179 | +# Monitor download speed |
| 180 | +curl -o /dev/null -w "%{speed_download}\n" http://example.com/large-file.zip |
| 181 | + |
| 182 | +# Test with time measurement |
| 183 | +time curl -O http://example.com/large-file.zip |
| 184 | +``` |
| 185 | + |
| 186 | +#### 2. Testing with wget |
| 187 | + |
| 188 | +```bash |
| 189 | +# Display download speed |
| 190 | +wget --progress=bar:force http://example.com/large-file.zip |
| 191 | + |
| 192 | +# Set timeout |
| 193 | +wget --timeout=30 http://example.com/large-file.zip |
| 194 | +``` |
| 195 | + |
| 196 | +#### 3. nginx Log Monitoring |
| 197 | + |
| 198 | +```bash |
| 199 | +# Check real-time access logs |
| 200 | +tail -f /var/log/nginx/access.log |
| 201 | + |
| 202 | +# Analyze bandwidth usage |
| 203 | +awk '{print $7, $10}' /var/log/nginx/access.log | sort | uniq -c |
| 204 | +``` |
| 205 | + |
| 206 | +### Error Handling and Troubleshooting |
| 207 | + |
| 208 | +#### Configuration Validation Script |
| 209 | + |
| 210 | +```bash |
| 211 | +#!/bin/bash |
| 212 | +# nginx bandwidth limiting configuration validation |
| 213 | + |
| 214 | +echo "=== nginx Configuration Syntax Check ===" |
| 215 | +sudo nginx -t |
| 216 | + |
| 217 | +if [ $? -eq 0 ]; then |
| 218 | + echo "✅ Configuration file syntax OK" |
| 219 | + |
| 220 | + echo "=== Configuration Reload ===" |
| 221 | + sudo nginx -s reload |
| 222 | + |
| 223 | + echo "✅ nginx configuration reload complete" |
| 224 | + |
| 225 | + echo "=== Test File Creation ===" |
| 226 | + sudo dd if=/dev/zero of=/var/www/html/test.dat bs=1M count=100 |
| 227 | + |
| 228 | + echo "✅ 100MB test file creation complete" |
| 229 | + echo "Test with: curl -O http://localhost/test.dat" |
| 230 | +else |
| 231 | + echo "❌ Configuration file contains errors" |
| 232 | + exit 1 |
| 233 | +fi |
| 234 | +``` |
| 235 | + |
| 236 | +#### Common Problem Resolution |
| 237 | + |
| 238 | +1. **Configuration not being applied** |
| 239 | + ```bash |
| 240 | + # Check nginx processes |
| 241 | + sudo ps aux | grep nginx |
| 242 | + |
| 243 | + # Check port usage |
| 244 | + sudo netstat -tlnp | grep :80 |
| 245 | + ``` |
| 246 | + |
| 247 | +2. **Too slow speed limiting** |
| 248 | + ```nginx |
| 249 | + # Guarantee minimum speed |
| 250 | + location / { |
| 251 | + limit_rate_after 1M; |
| 252 | + limit_rate 100k; # Guarantee minimum 100KB/s |
| 253 | + } |
| 254 | + ``` |
| 255 | + |
| 256 | +## Conclusion |
| 257 | + |
| 258 | +Using nginx's `limit_rate` and `limit_rate_after` directives allows for effective bandwidth limiting. Key insights include: |
| 259 | + |
| 260 | +- **Progressive Limiting**: Use `limit_rate_after` for fast initial downloads, then apply speed limits |
| 261 | +- **File Type Differentiation**: Apply different limits for media files and general files |
| 262 | +- **Real-time Monitoring**: Track bandwidth usage through log analysis |
| 263 | + |
| 264 | +Next steps could include considering nginx Plus's advanced bandwidth control features or more granular control using dynamic modules. |
0 commit comments