http 500 errors are not cached but go to the backend

Maxim Dounin mdounin at
Fri Jan 4 22:31:05 UTC 2013


On Fri, Jan 04, 2013 at 08:38:53AM -0500, brama wrote:

> I've set up nginx (tested with 1.2.6 and 1.3.10) to cache all requests to
> our fastcgi backends. If a cache entry expires, stale entries wil be served
> while nginx is updating the cache. I'm using the fastcgi_cache_lock feature
> to make sure only 1 request will be sent to the backend to update the
> cache.

Just a side note: the fastcgi_cache_lock directive doesn't affect 
update of the cache, it only affects adding new items to the 
cache.  To handle cache updating the "fastcgi_cache_use_stale 
updating" should be used (it's actually already in your config).


> This seems counter-intuitive. If the http 500 document expires, nginx should
> serve the stale cache entry until it has updated the cache entry. However,
> with http_500 included in fastcgi_cache_use_stale, it never appears to
> update the cache entry at all. Bug?


   fastcgi_cache_use_stale http_500;

in your config instructs nginx to don't cache 500 response but 
return stale cached content instead.  As soon as original cached 
resource expires - nginx starts to ask backend about new response, 
but since 500 is returned it returns stale response to clients 

In your case the behaviour looks a bit confusing as "original 
cached resource" above is the same 500 response (cached as 
fastcgi_cache_use_stale don't affect initial content caching), but 
the behaviour is as expected with your configuration - when you 
ask nginx to cache 500 responses and to don't use 500 responses 
for cache update at the same time.

Maxim Dounin

More information about the nginx mailing list