Two of the cheapest speed-ups for any website are compression (sending fewer bytes) and caching (not sending them again). Both are handled by Nginx with a few lines of configuration and benefit every visitor — most of all those on mobile networks or far from your server, such as visitors in mainland China reaching a server in Hong Kong or the US. This guide covers compression, HTTP/2 and HTTP/3, browser caching headers, and how to check that it all works.
gzip compression
Text formats — HTML, CSS, JavaScript, JSON, SVG — typically shrink by 60–80% with gzip. Nginx’s gzip module is included in standard packages; it is just not fully enabled by default.
Create /etc/nginx/conf.d/gzip.conf:
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
application/atom+xml
application/manifest+json
image/svg+xml
font/ttf
font/otf;
| Setting | Why |
|---|---|
gzip_comp_level 5 |
Good balance; levels above 6 cost CPU for little gain |
gzip_min_length 256 |
Tiny responses are not worth compressing |
gzip_vary on |
Adds Vary: Accept-Encoding so proxies cache correctly |
gzip_proxied any |
Compress responses to proxied requests (e.g. behind a CDN) |
text/html is always compressed when gzip is on, so it is not listed. Do not compress JPEG, PNG, WebP, MP4, ZIP or WOFF2 — they are already compressed.
If you already have gzip lines in /etc/nginx/nginx.conf, remove or comment them out to avoid “duplicate directive” errors.
sudo nginx -t && sudo systemctl reload nginx
Brotli: better compression than gzip
Brotli compresses text roughly 15–25% smaller than gzip at similar speed, and all modern browsers support it over HTTPS. Nginx does not include it by default; it is a module.
On Debian/Ubuntu, packaged modules are available in recent releases:
apt search libnginx-mod-brotli
sudo apt install libnginx-mod-http-brotli-filter libnginx-mod-http-brotli-static
(If the packages are not available for your release, the Nginx mainline repository or building the ngx_brotli module yourself are the alternatives.)
Then /etc/nginx/conf.d/brotli.conf:
brotli on;
brotli_comp_level 5;
brotli_types
text/plain text/css text/javascript application/javascript application/json
application/xml application/rss+xml image/svg+xml application/manifest+json;
Keep gzip enabled as well: browsers that do not offer Brotli get gzip.
Pre-compressed files
For static sites, compress files once at build time and let Nginx serve them without CPU cost:
gzip_static on;
brotli_static on;
Nginx then serves app.js.br or app.js.gz if it exists next to app.js.
HTTP/2 and HTTP/3
HTTP/2 sends many files over one connection in parallel, removing the need for old tricks like domain sharding. It requires HTTPS.
# Nginx 1.25.1 and newer
server {
listen 443 ssl;
http2 on;
...
}
# Older Nginx
server {
listen 443 ssl http2;
...
}
HTTP/3 (QUIC, over UDP) is supported from Nginx 1.25 when built with QUIC support. It helps on lossy mobile networks:
listen 443 quic reuseport;
listen 443 ssl;
http2 on;
add_header Alt-Svc 'h3=":443"; ma=86400';
Open UDP port 443 in the firewall. Check your Nginx with nginx -V 2>&1 | grep -o with-http_v3_module. HTTP/3 is optional; HTTP/2 already gives most of the benefit.
Also make sure TLS 1.3 is enabled (ssl_protocols TLSv1.2 TLSv1.3;) — it saves a round trip on every new connection.
Browser caching with Cache-Control
When a browser caches a file, returning visitors and every subsequent page skip the download entirely. The rule:
- Versioned static assets (file names that change when content changes, e.g.
app.3f9a2c.js): cache for a year and mark immutable. - Unversioned static files (images,
style.css): cache for days or weeks. - HTML: do not cache for long, so updates appear immediately.
# Versioned build assets (hash in file name)
location ~* \.(?:js|css)$ {
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
}
# Images, fonts, media
location ~* \.(?:jpg|jpeg|png|gif|webp|avif|svg|ico|woff2?|ttf|mp4|webm)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000";
access_log off;
}
# HTML
location ~* \.html$ {
add_header Cache-Control "no-cache";
}
no-cache does not mean “never cache”: the browser keeps a copy but checks with the server (using ETag / Last-Modified) before using it, getting a tiny 304 response if nothing changed.
Only use immutable and one-year lifetimes when file names really change with content. WordPress adds ?ver= query strings to many assets, which works for cache busting too.
A common pitfall: add_header in a location block replaces all add_header directives from the server block (such as security headers). Repeat the headers you need in each location, or put them in a snippet that you include.
Open file cache
For sites with many static files, let Nginx cache file descriptors and metadata:
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 120s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
Put it in the http block.
Other quick wins
# In the http block
sendfile on;
tcp_nopush on;
keepalive_timeout 30s;
server_tokens off;
These are already the default in many distribution configs; check /etc/nginx/nginx.conf.
Verify everything
Compression:
curl -s -o /dev/null -w "%{size_download} bytes\n" https://example.com/
curl -s -H "Accept-Encoding: br,gzip" -o /dev/null -w "%{size_download} bytes\n" https://example.com/
curl -sI -H "Accept-Encoding: br,gzip" https://example.com/style.css | grep -i content-encoding
HTTP/2:
curl -sI --http2 https://example.com/ | head -1 # HTTP/2 200
Caching headers:
curl -sI https://example.com/images/logo.webp | grep -iE "cache-control|expires"
And in the browser: DevTools → Network, reload twice. The second load should show many files “from disk cache” or “memory cache”, and the “Content-Encoding” column (right-click the header to add it) should show br or gzip for text files.
Results to expect
| Change | Typical effect |
|---|---|
| gzip on text | HTML/CSS/JS 60–80% smaller |
| Brotli instead of gzip | A further ~15–25% smaller |
| HTTP/2 | Faster pages with many assets, especially at high latency |
| Long browser caching | Repeat views load almost entirely from cache |
These matter most where latency is high or bandwidth limited — for example visitors in mainland China on a premium but narrow CN2 GIA line, or mobile users. Combine them with the steps in speeding up a website for China and WordPress optimisation.