Take the 2-minute tour ×

I've configured my system with NginX listening on port 80, serving static content and proxying dynamic requests to a backend server.

I can configure NginX to cache content generated by the backend, but I want this cached content be served only when the Backend responds with an error http 5xx, or when it's totally down.

We tried the proxy_cache_use_stale option with max-age of 1 second, it worked but it has one negative side.. which is simply dozens of requests being served from cache during this 1 second cache-aged-content. These requests served from cache will miss further Backend processing (Stats for example).

We can only afford to live with this negativity IF the backend was down, Thus, the cache will act as a backup or a failover solution. But as long as the backend is up and responding, no requests should be served from cache.

I would appreciate any hints

share|improve this question
@Danack Thanks a lot for the corrections. I got disconnected when I was trying to correct the typos.. :( But, Thanks again. –  Hasan S. May 28 '13 at 14:58
add comment

1 Answer

Take a look at proxy_cache_use_stale

share|improve this answer
This is regard using Stale, means: When to use Stale version of the original cached content? What I need is to never use cached content unless got some error response code from backend. Please.. correct me if I'm wrong. –  Hasan S. May 28 '13 at 15:01
Think about it. How would a cache serve something it has never stored? If you set a max-age of one second, you will achieve your goal. –  Melvyn May 28 '13 at 16:39
Oh, Cool.. Great Hint.. I'll give a try, and will post updates ;-) –  Hasan S. Jun 1 '13 at 9:07
I made few tries, unfortunately.. the server will serve few requests from cache before the 1 second expire. What I need is: the Nginx caches the request upon first request.. and keep serving future request directly via backends unless got an error response code. Here comes the Cached Content. I tried to make the 1 second to be 1 MS, but seems the minimum is 1 second. –  Hasan S. Jun 3 '13 at 16:08
Could you explain a bit more about the setup? Maybe nginx is not the right tool for the job. What breaks when you serve one second old content? And why is it still ok to serve thirty minutes old content when the backend is unresponsive? –  Melvyn Jun 3 '13 at 16:23
show 1 more comment

Your Answer


