Nginx Caching Configuration

Nginx proxy caching can absorb traffic spikes by serving responses from disk without hitting your backend. Configure cache zones, TTLs, and bypass conditions with our generator.

A proxy cache zone

Declare the zone in http, then enable it per location with a TTL per status code.

proxy_cache_path /var/cache/nginx keys_zone=app:10m max_size=1g inactive=60m;
location / {
    proxy_cache app;
    proxy_cache_valid 200 10m;
    proxy_cache_use_stale error timeout updating;
    add_header X-Cache-Status $upstream_cache_status;
}

Do not cache personalised responses

Skip the cache for requests with cookies or authorization headers using proxy_cache_bypass and proxy_no_cache, or you can serve one user’s page to another.

Open the Static site generator

Frequently asked questions

How do I see whether a response was cached?

Expose $upstream_cache_status as a response header and look for HIT, MISS or EXPIRED.

What is the difference between proxy_cache and fastcgi_cache?

Same mechanism; proxy_cache stores upstream HTTP responses, fastcgi_cache stores PHP-FPM responses.

Guides