Controlling Varnish cacheable content

There are three ways to prevent content being cached in Varnish.

Add an explicit bypass in the Varnish configuration.

At the moment, only our support team can modify the Varnish configuration. You should supply your exception rules in a support ticket in a suitable REGEX format.

Send the appropriate no-cache header in the response

You can do this yourself by setting a cookie in your application nocache=1

Add an Nginx header to define the element as being non-cacheable

You can edit the Nginx configuration (eg. ___general/example.com.conf) (replace the domain as necessary) to add specific rules for content to be bypassed from being cached.

You can get very granular using regular expressions, and ultimately control exactly what does and doesn't get cached. The important command is just a single line, set $magestack_cacheable false; - which you can use in any location.

Customising TTLs

You can also define specific cache TTLs for a given URI just be appending another header, suitable format is as per Varnish configuration syntax.

set $magestack_cache_lifetime 5d;

Where the value follows uses the following syntax

[0-9]+[s|m|h|d|w]  -  seconds|minutes|hours|days|weeks

Examples

Enable Varnish for dynamic content

In ___general/example.com.conf

# Enable Varnish
set $magestack_cacheable true;

# Disable Varnish on specific URIs
location ~* ^/(index.php/)?(checkout|customer) {
  set $magestack_cacheable false;
}

Enable Varnish for static content

Static content is classed as any file defined here.

In ___general/example.com.static.conf

# Enable Varnish
set $magestack_cacheable true;

Enable Varnish for semi-static content

Semi static content is classed as any file ending in js|css

In ___general/example.com.semi-static.conf

# Enable Varnish
set $magestack_cacheable true;

Enable Varnish except for logged in WordPress users

In ___general/example.com.conf

# Enable Varnish
set $magestack_cacheable true;

# Disable Varnish for logged in WordPress admins
if ($http_cookie ~ "wordpress_logged_in_[a-fA-F0-9]+=admin") {
  set $magestack_cacheable false;
}

Enable Varnish except for WordPress admin

In ___general/example.com.conf

# Enable Varnish
set $magestack_cacheable true;

# Disable Varnish for WordPress admin
location ~* ^/wp-(login|admin)|wp-*\.php$ {
  set $magestack_cacheable false;
  location ~* \.(php) {
    set $magestack_cacheable false;
    include fastcgi_params;
  }
  try_files $uri $uri/ index.php;
}

Disable Varnish for a real PHP file

Paths to real PHP files (rather than a virtual URL like /checkout) must include the nested location for php files, otherwise, Nginx will not interpret the file type properly and will cause the file to be downloaded rather than processed.

You should always nest the following location, inside of your new location, if it may match a real PHP file

location ~* \.(php) {
  include fastcgi_params;
}

In ___general/example.com.conf

# Enable Varnish
set $magestack_cacheable true;

# Disable Varnish for real php file
location ~* ^/custom-script.php {
  location ~* \.(php) {
    set $magestack_cacheable false;
    include fastcgi_params;
  }
}

! Varnish cache is disabled by default for all content, it must be enabled at the top of your ___general/example.com.conf before use.