Fixing "Leverage browser caching" in PageSpeed Insights

If you see a low PageSpeed Insights score and see warnings about “Leverage browser caching” in the list of items to improve:

Pagespeed Inisghts requires you to set browser time for over 7 days.
“We recommend a minimum cache time of one week and preferably up to one year for static assets,”

You can use Varnish to fix the cache-control header before sending to the browser:

You can use the above code in conjunction with static caching code to improve both PageSpeed Insights score, page load time as well as reduce data usage to the origin.

Keep in mind that the above fixes only work for assets that are served from the domain that Varnish is serving, not 3rd party domains. So if your site is, then that above fix will work for all assets from, it won’t have affect on assets from as an example.