I've put Nginx in front of a php app server as a reverse proxy to cache the dynamic content and save some hits to php.
I've configured Nginx with the settings below with the hope that I will achieve the following behavior.
1) Nginx will cache 200 status code content for 20m before it attempts to refetch from the app servers
2) The cache will remain in place for up to 7 days to handle stale requests for infrequently accessed content.
3) Requests that come in 20 minutes after the content was originally cached will fire off a request to the backend for a fresh update, but will serve the stale version so the client get's an instant response.
proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=staticfilecache:512m inactive=7d max_size=15000m; proxy_cache_use_stale timeout updating error invalid_header; proxy_cache_valid 200 20m; proxy_cache_valid 404 1m; proxy_cache_valid any 15m;
UPDATE: After running some more tests and watching the server logs, it appears that content that is more than 20m old is not being served from the cache as stale, but instead is just building up in the cache_file_system.
Any ideas how to get Nginx to serve stale content for an extended period of time? The use case is basically caching infrequently accessed content that is expensive to generate on my app servers. Being able to serve stale items several days after they have expired in the cache when a hit finally comes in would be a great performance boost. If this isn't possible with Nginx, I'm open to other proxy/caching options (I'm only using this Nginx instance as a cache).