Back to home

Varnish cache: How to control cache with HTTP headers

Varnish processes the HTTP header in order to decide whether it should cache the content or not.

Request header

In case of an incoming request Varnish checks for cookies. All request containing a cookie will not be cached by default.

If the content schould be cached anyway there are two possibilities:
For static content a seperate domain where there are no cookies in use should be implemented. For example: static.example.com

Alternatively, cookies can be removed using the Varnish configuration file.
You can remove cookies on all or just some specific requests. We would be pleased to engineer a Varnish configuration tailored to your needs. Please contact our support on support@nine.ch or by phone +41 44 637 40 40.

Response header

By using the following HTTP headers in the response of the webserver you can control the Varnish caching. The headers can be set in your web application or in case of Apache by using an .htaccess file.

Cache-Control

The value of s-maxage or if not available max-age tells Varnish for how long the content should be cached. All other values like public or no-cache will be ignored up to and including Varnish 3. This header always comes before Expires.

Cache-Control: public, max-age=1372, s-maxage=1072

By using the mod_headers module for Apache these settings can be set by using an .htaccess file.

<IfModule mod_headers.c>
  Header append Cache-Control "public, max-age=1372, s-maxage=1072"
</IfModule>

Expires

By using the expires header you can define until when a file will be cached by Varnish. This header won’t be processed if Cache-Control is set.

Expires : Thu, 16 Apr 2015 08:43:41 GMT

By using the mod_expires module for Apache you can specify this header by using an .htaccess file. For multiple file types this would look as follows:

<IfModule mod_expires.c>
  ExpiresActive on

  # Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault      "access plus 1 month"

  # Your document html
  ExpiresByType text/html "access plus 0 seconds"

  # Favicon (cannot be renamed)
  ExpiresByType image/x-icon "access plus 1 week"

  # Media: images
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
</IfModule>

Vary

With this header you can define that based on which request headers a site can have multiple versions. Because of that it’s possible for a site to have multiple versions varying in regard of Accept-Language or User-Agent. In case of too many different version the caching of the site can get very difficult.

Vary: Accept-Language

By using the mod_headers module for Apache these settings can be set by using an .htaccess file as follows:

<IfModule mod_headers.c>
  Header append Vary "Accept-Language"
</IfModule>

Didn't find what you were looking for?

Contact our support:

+41 44 637 40 40 support@nine.ch