0

Lets say my backend takes a horrific 2 seconds to respond (out of our control say), and I never EVER want a visitor to experience this delay, we are not influenced by cookies or user session.

What I want to do in nginx is something like this:

server {
  location / {
    # server requests from cache unless for some reason its not in cache
    proxy_pass http://backend/
    proxy_cache mycache
    proxy_cache_is_valid 2m
  }

  location / where request IP = 127.0.0.1 {
     proxy_pass http://backend/
     do_not_use_cache
     but_store response in cache
  }
}

This way I can have a simple curl task from localhost run every 30 seconds that keeps the cache fresh/hot with the several pages I need, I never want the visitor to be the one that warms up the cache. I've read the docs but cant see how to make this happen.

Martin Thoma
  • 277
  • 4
  • 13
  • I dont see how this differs from any typical caching scenario, can you please elaborate? – 3molo Jun 12 '11 at 04:38
  • Visitors keep the cache hot in normal scenario so some will inevitably hit the backend and experience the 2sec delay. I never want visitors to hit the backend. Something has to keep the cache hot. – Mâtt Frëëman Jun 12 '11 at 04:42

1 Answers1

3

Try this configuration.

http {
  # http section directives

  proxy_cache_path /path/to/cache levels=1:2 keys_zone=mycache;

  geo $bypass_by_ip {
    default   0;
    127.0.0.1 1;
  }

  server {
    # other server directives

    location / {
      proxy_cache mycache;
      proxy_cache_valid any 2m;
      proxy_cache_use_stale timeout updating;
      proxy_cache_bypass $bypass_by_ip;

      proxy_pass ...;
    }
  }
}

proxy_cache_bypass makes a direct request, bypassing the cache. This is controlled by its argument(s) -- if any of them is not an empty string or 0. I am using geo to provide such a value (0 by default and 1 when remote IP is 127.0.0.1)

NOTE you need Nginx version 1.0.4 at least for this configuration to work. Earlier versions had a bug in proxy_cache_bypass / proxy_cache_nocache logic.

Alexander Azarov
  • 3,550
  • 21
  • 19
  • The bypassing the cache on the way in does it add the result to the cache on the way back out? – Mâtt Frëëman Jun 12 '11 at 13:17
  • Since `proxy_cache_nocache` explicitly prohibits caching the response, I believe yes, `proxy_cache_bypass` should add the response to the cache. But you definitely need to test this, since I didn't use this scenario. – Alexander Azarov Jun 12 '11 at 17:46
  • Thanks Alaz, will wipe up a simple datetime stamp php page to test and report the results – Mâtt Frëëman Jun 13 '11 at 09:11