location /dynamic/ {
proxy_cache my_cache;
proxy_cache_valid 200 1s;
proxy_pass http://backend;
}The proxy_cache_valid 200 1s; directive caches successful (200) responses for 1 second. This short caching period is called micro-caching and helps reduce backend load for frequently requested dynamic content.
Setting proxy_cache_methods GET; ensures only GET requests are cached. The proxy_cache_valid 200 2s; caches successful responses for 2 seconds.
proxy_cache_valid 200 1s; but no caching occurs. Which is the most likely cause?If the backend sends headers like Cache-Control: no-cache, nginx will not cache the response despite proxy_cache_valid settings.
First define cache storage with proxy_cache_path, then enable caching in location with proxy_cache, set cache duration, and finally reload nginx.
Caching GET requests for a short time like 5 seconds while respecting backend cache headers balances freshness and load effectively.