Configuring Varnish to rewrite Expires/Cache-Control headers for client-side caching

cacheperformancevarnish

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.

Best Answer

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 = "...";
  }
  ..
}