What’s the correct nginx config for cache-control and server-caching rules?
Asked on Oct 09, 2025
Answer
To optimize web performance with Nginx, setting appropriate cache-control and server-caching rules is essential. This involves configuring headers to instruct browsers on how to handle caching for different types of content.
<!-- BEGIN COPY / PASTE -->
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
location ~* \.(html|htm)$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
}
<!-- END COPY / PASTE -->Additional Comment:
- Use "expires" to set how long resources should be cached by the browser.
- "Cache-Control" headers provide more granular control over caching behavior.
- Static assets like images, CSS, and JS can be cached for longer durations (e.g., 30 days).
- Dynamic content like HTML should have caching disabled to ensure users receive the latest version.
- Adjust paths and file types according to your specific needs and website structure.
Recommended Links: