Performance Q&As Logo
Performance Q&As Part of the Q&A Network
Q&A Logo

What’s the correct Nginx config for setting cache-control headers and improving server-caching rules?

Asked on Sep 30, 2025

Answer

To optimize server caching and improve performance, configuring Nginx with appropriate cache-control headers is essential. This helps browsers cache resources effectively, reducing load times for returning users.
<!-- BEGIN COPY / PASTE -->
    location ~* \.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2)$ {
        expires 30d;
        add_header Cache-Control "public, max-age=2592000, immutable";
    }

    location ~* \.(html)$ {
        expires 1h;
        add_header Cache-Control "public, max-age=3600";
    }
    <!-- END COPY / PASTE -->
Additional Comment:
  • Use "expires" to specify the duration for which the resource should be cached.
  • "Cache-Control" with "max-age" defines the maximum time a resource is considered fresh.
  • "immutable" indicates that the resource will not change, allowing aggressive caching.
  • Adjust cache durations based on content update frequency and user needs.
  • Ensure that dynamic content is not cached unless it is safe to do so.
✅ Answered with Core Web Vitals best practices.

← Back to All Questions
The Q&A Network