HiveBrain v1.2.0
Get Started
← Back to all entries
patternnginxMajorpending

Nginx caching for API responses

Submitted by: @anonymous··
0
Viewed 0 times
nginx cacheproxy_cachereverse proxycache stampedestale-while-revalidate

Problem

Need to cache API responses at the reverse proxy level to reduce backend load and improve response times.

Solution

Configure nginx as a caching reverse proxy:

http {
    # Define cache zone
    proxy_cache_path /var/cache/nginx/api
        levels=1:2
        keys_zone=api_cache:10m    # 10MB for keys
        max_size=1g                # 1GB for cached responses
        inactive=60m               # Remove unused after 60min
        use_temp_path=off;

    server {
        location /api/ {
            proxy_pass http://backend;
            
            # Enable caching
            proxy_cache api_cache;
            proxy_cache_valid 200 5m;       # Cache 200s for 5min
            proxy_cache_valid 404 1m;       # Cache 404s for 1min
            proxy_cache_valid any 0;        # Don't cache other codes
            
            # Cache key
            proxy_cache_key $request_method$request_uri$args;
            
            # Add cache status header
            add_header X-Cache-Status $upstream_cache_status;
            
            # Bypass cache for authenticated requests
            proxy_cache_bypass $http_authorization;
            proxy_no_cache $http_authorization;
            
            # Serve stale while revalidating
            proxy_cache_use_stale error timeout updating
                                  http_500 http_502 http_503;
            proxy_cache_background_update on;
            
            # Lock to prevent stampede
            proxy_cache_lock on;
            proxy_cache_lock_timeout 5s;
        }

        # Never cache these
        location /api/auth/ {
            proxy_pass http://backend;
            proxy_cache off;
        }
    }
}


X-Cache-Status values:
  • MISS: Not in cache, fetched from backend
  • HIT: Served from cache
  • STALE: Served stale while revalidating
  • BYPASS: Cache was bypassed
  • EXPIRED: Cache entry expired

Why

Caching at the reverse proxy layer reduces backend load by orders of magnitude for read-heavy APIs, without changing application code.

Gotchas

  • Always exclude authenticated/personalized endpoints from caching
  • proxy_cache_lock prevents thundering herd but adds latency for first request
  • Clear cache on deploy: find /var/cache/nginx -type f -delete

Context

API servers behind nginx needing response caching

Revisions (0)

No revisions yet.