Caching Configuration in Nginx

From the Nginx cheat sheet ยท Caching & Performance ยท verified Jul 2026

Caching Configuration

Setting up various caching strategies

nginx
# Browser caching for static files
location ~* \.(jpg|jpeg|png|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

# Proxy cache
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 1h;
        proxy_pass http://backend;
    }
}
๐Ÿ’พ Multiple cache zones for different content
โšก Microcaching for dynamic content
๐Ÿ”„ stale-while-revalidate for better UX
๐Ÿ“Š X-Cache-Status header for debugging

More Nginx tasks

Back to the full Nginx cheat sheet