NGINX Caching and Compression: proxy_cache, Browser Caching and gzip
Speed up sites with NGINX: browser caching headers for static files, proxy_cache for backend responses, microcaching, gzip and Brotli compression.
Published November 27, 2026
The fastest request is the one your application never sees. NGINX can tell browsers to keep static files for months, store backend responses and serve them to thousands of visitors, and compress text so pages download several times faster. These are some of the cheapest performance wins available to any website.
The concept
There are two separate caches to think about. The browser cache lives on each visitor's device and is controlled by response headers such as Cache-Control and Expires. The proxy cache lives on the NGINX server and stores responses from your backend, so repeated requests for the same URL never reach the application.
Compression is complementary. gzip (built in) and Brotli (a module) shrink HTML, CSS, JavaScript, JSON and SVG by 60-80%. Images, videos and fonts like WOFF2 are already compressed, so compressing them again wastes CPU.
Layers of caching
The effect of caching
Browser caching for static assets
Assets with a content hash in the filename (app.3f9a1c.js) never change, so they can be cached for a year and marked immutable. HTML should be revalidated so users get new deployments.
location ~* \.(?:css|js|woff2|svg|png|jpg|jpeg|webp|avif|ico)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
location / {
add_header Cache-Control "no-cache"; # revalidate HTML every time
try_files $uri $uri/ /index.html;
}Proxy caching backend responses
proxy_cache_path defines where and how much to store; proxy_cache enables it per location. The X-Cache-Status header makes it easy to see HITs and MISSes while testing.
http {
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=app:50m
max_size=2g inactive=60m use_temp_path=off;
server {
location / {
proxy_cache app;
proxy_cache_valid 200 301 10m;
proxy_cache_valid 404 1m;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503;
proxy_cache_lock on;
proxy_cache_bypass $cookie_session;
proxy_no_cache $cookie_session;
add_header X-Cache-Status $upstream_cache_status;
proxy_pass http://127.0.0.1:3000;
}
}
}Practical examples
Enable gzip properly
Compress text types only, with a sensible minimum size.
gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/json
image/svg+xml application/xml application/rss+xml;Microcaching a dynamic site
Even a 1-second cache absorbs traffic spikes: 1,000 requests per second become one backend request per second.
location / {
proxy_cache app;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
proxy_cache_lock on;
proxy_pass http://app;
}Check that it works
Look for content-encoding and cache status headers.
curl -sI -H 'Accept-Encoding: gzip' https://example.com/app.js | grep -i content-encoding
for i in 1 2; do curl -sI https://example.com/ | grep -i x-cache-status; done
# X-Cache-Status: MISS
# X-Cache-Status: HITCommon mistakes
- Caching pages that contain user-specific data and showing one user's account to another. Bypass the cache for authenticated requests.
- Long browser cache times on files without hashed names, so users keep stale CSS after a deploy.
- Compressing images and fonts that are already compressed.
- Forgetting that the backend's Set-Cookie or Cache-Control: private headers stop NGINX from caching by default.
- No proxy_cache_lock, so a cache expiry sends a stampede of identical requests to the backend.
Key takeaways
- Use long immutable caching for hashed static files and no-cache for HTML.
- proxy_cache stores backend responses on the NGINX server; even 1-second microcaching helps.
- Never cache personalised responses; bypass on session cookies.
- gzip text types only and verify with curl headers.
FAQ
How do I purge the NGINX cache?
Open source NGINX has no purge API. Delete files under the cache path (or the whole directory) or use a short validity. NGINX Plus and the ngx_cache_purge module add purge support.
Should I use Brotli?
Brotli compresses text 15-25% better than gzip. It needs the ngx_brotli module; keep gzip enabled as a fallback for older clients.
Does a CDN replace NGINX caching?
A CDN caches at edge locations near users. NGINX caching still protects your origin from requests the CDN does not cache or that miss at the edge.