9

I am hoping someone can advise on the proper method for getting Varnish to send cache-control headers. Currently, my configuration is sending "Cache-Control: no-cache" to clients.

Thanks in advance to anyone who might be able to help...

Kevin Gleeson
  • 113
  • 1
  • 1
  • 7
  • It might help if you explain what you want. Which configuration (varnish or your back-end) is sending no-cache headers? You can override these headers in Varnish, but whether this is 'correct' depends on the semantics of your responses (are they reusable, over clients, for how long, etc). – ivy Jan 25 '12 at 22:27
  • @ivy, Thanks for your reply. What I am trying to accomplish is to have **Varnish** send cache-control headers which I manually specify, regardless of what the back-end sends to Varnish. – Kevin Gleeson Jan 26 '12 at 15:29

3 Answers3

15

Your back-end is sending "Cache-Control: no-cache" to Varnish which implies two things:

  • Varnish will not store the response in the cache (so a next lookup will fail)
  • Your clients (browsers and intermediate proxies) will not cache responses (and request them over and over).

The solution is simple: remove the cache-control headers after fetching the response from the back-end (and before storing them in the cache).

In your vcl file do:

sub vcl_fetch {
  remove beresp.http.Cache-Control;
  set beresp.http.Cache-Control = "public";
}

You can choose to only do this for certain urls (wrap it in ( if req.url ~ "" ) logic) and do way more advanced stuff.

ivy
  • 5,539
  • 1
  • 34
  • 48
4

Varnish ignores Cache-Control: nocache as per the documentation. Here is evidence confirming that:

http://drupal.org/node/1418908

To get that result, you should detect the header Cache-Control .nocache. from your backend, and then invalidate the cache, set the backend response to not cacheable, or issue max-age: 0 in the other header (I forget the name right now).

Rudd-O
  • 116
  • 1
  • 3
1

[ivy] has good advice, and/but it gets a little complicated when you try to obey a servers intent for end user (browser) caching. I found this resource to be helpful in understanding a way to configure Varnish to hold onto a cache longer than a browser is instructed to...

https://www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching

Daniel
  • 2,869
  • 4
  • 26
  • 28
Nate-
  • 157
  • 1
  • 9