Caching and Compression

Harry · 12 Sep 2026 · 9 views

Proxy Caching - Fewer Hits to the App

Let Nginx serve cached copies of expensive responses:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g;

server {
    location / {
        proxy_cache my_cache;
        proxy_cache_valid 200 60m;
        proxy_cache_use_stale error timeout updating;
        add_header X-Cache-Status $upstream_cache_status;
    }
}

Check the X-Cache-Status header: HIT, MISS, STALE. Set cache keys with proxy_cache_key when args matter.

Respect App Cache Headers

location /api/user/ {
    proxy_cache_valid 200 5m;
    proxy_no_cache $http_authorization;   # never cache authenticated calls
}

Compression

gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_types text/plain text/css application/json application/javascript application/xml image/svg+xml;

With brotli installed you may instead use brotli on; brotli_types ...; - even better ratios.

Cache-Control for Clients

location ~* \.(css|js|svg|webp|woff2)$ { expires 7d; }
location ~* \.(html|json)$ { expires 10m; }

Key Points

  • Proxy caching at nginx turns repeated app calls into passive responses.
  • Never cache personalized or authenticated content.
  • gzip/brotli + expires headers are pure wins, configure once.
Share this post:

Comments (0)

Please login or register to comment.