Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I configure Varnish so that all responses to requests matching say "*.css" gets cached in the client's browser for 30 days?

What I'm trying to achieve is making Varnish set the correct "Expires:" and/or "Cache-Control" settings so that all CSS:s are cached for 30 days regardless of what the backend says about the client-side cacheability of these objects.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

This does the trick:

sub vcl_fetch {
  ..
  if (req.url ~ "\.css$") {
    set obj.http.magicmarker = "1";
  }
  ..
}

sub vcl_deliver {
  ..
  if (resp.http.magicmarker) {
    unset resp.http.magicmarker;
    set resp.http.Cache-Control = "...";
    set resp.http.Expires = "...";
    set resp.http.Age = "...";
  }
  ..
}
share|improve this answer
add comment

I don't think that Varnish will let you scope the VCL rules to "*.css". You may have to ask on the Varnish mailing list for exact info on this. Scoping by MIME type should be possible.

Assuming your objects already have cache headers set but you wish to normalize them, then a modified version of this Varnish documentation VCL should help you: http://varnish.projects.linpro.no/wiki/VCLExampleLongerCaching

In general, Varnish was not built to do larger amounts of header or content rewriting. Your web servers or web application should do this better than Varnish.

share|improve this answer
add comment

I just need to point out that varnish is actually excellent for rewriting headers. Testing for any url is possible in vcl_recv, and in vcl_fetch, it is a common application for varnish. In recv, you analyze an incoming request. In fetch you analyze the backend response.

Generally, you manipulate the request in recv, and direct it to a backend, and you manipulate the response in fetch, and rewrite headers to suit, before storing the object in the cache potentially, and delivering the response.

In fetch you can do

unset beresp.http.expires;
unset beresp.http.cache-control;

as well as

set beresp.http.Cache-Control = "max-age = 2678406";

and so on.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.