11

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

Hasan Alsawadi
  • 394
  • 5
  • 15
  • 1
    @Danack Thanks a lot for the corrections. I got disconnected when I was trying to correct the typos.. :( But, Thanks again. – Hasan Alsawadi May 28 '13 at 14:58

2 Answers2

5

Take a look at proxy_cache_use_stale

  • 1
    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 Alsawadi 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. –  May 28 '13 at 16:39
  • Oh, Cool.. Great Hint.. I'll give a try, and will post updates ;-) – Hasan Alsawadi Jun 01 '13 at 09:07
  • 1
    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 Alsawadi Jun 03 '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? –  Jun 03 '13 at 16:23
0

proxy_intercept_errors might be what you're looking for.

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_intercept_errors

chugadie
  • 2,786
  • 1
  • 24
  • 33