5 Simple Nginx Tweaks to Slash Bandwidth and Boost Performance
During the COVID‑19 surge, web traffic spiked dramatically, prompting a detailed guide on five practical Nginx configuration changes—enabling Gzip compression, setting cache headers, activating HTTP/2, streamlining logging, and limiting bandwidth—to dramatically reduce bandwidth usage and improve site responsiveness.
Admiral William McLaven once said that changing the world starts with making your bed; similarly, tiny adjustments to your HTTP server configuration can have a huge impact, especially during the COVID‑19 pandemic when internet traffic surged.
Consider an e‑commerce site running Nginx 1.15.9 serving 100 daily users, with 30% repeat visitors and an average of four page views per session. Optimizing the server can significantly reduce bandwidth and load.
1. Enable Gzip Compression
Enabling Gzip Compression for HTML, CSS, and JavaScript Files
Compressing text files on the fly saves bandwidth. Use the browser’s developer tools (F12) to verify compression; uncompressed files may be 1.15 MB, while compressed versions drop to about 260 KB—a reduction of roughly 80 % and saving up to 62 MB per day.
gzip on;
gzip_types application/xml application/json text/css text/javascript application/javascript;
gzip_vary on;
gzip_comp_level 6;
gzip_min_length 500;2. Set Cache Headers
Setting Cache Headers
Configure browsers to cache static assets like fonts and images for a month, reducing repeated requests.
location ~* \.(jpg|jpeg|gif|png|ico|woff2)$ {
expires 1M;
add_header Cache-Control "public";
}3. Enable HTTP/2 Protocol
Enabling HTTP/2 Protocol Support
HTTP/2 uses fewer TCP connections, improving network utilization. Enable it in nginx.conf by adding http2 to the listen directive and ensure TLS is active.
listen 443 ssl http2;4. Optimize Logging
Optimizing Logging
Reduce unnecessary log entries to save disk, CPU, and I/O. Three approaches:
[1] Disable logging for static resources
location ~* \.(jpg|jpeg|gif|png|ico|woff2|js|css)$ {
access_log off;
}[2] Log only error responses
# /etc/nginx/nginx.conf
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /var/log/nginx/access.log combined if=$loggable;[3] Buffer I/O operations
access_log /var/log/nginx/access.log combined buffer=512k flush=1m;5. Limit Bandwidth
Limiting Bandwidth for Particular URLs
Use limit_rate to cap download speed for large or popular files, preserving bandwidth for critical services.
location /download/ {
limit_rate 50k;
}Optionally apply limits only after a certain size:
location / {
limit_rate_after 500k;
limit_rate 50k;
}Note that rate limiting applies per HTTP connection, so users cannot bypass it with download managers.
Translated from: “Help the World by Healing Your NGINX Configuration”
These five techniques can dramatically improve site performance, though exact gains vary per site; collectively, they help the global network operate more efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
