Using Varnish With WP Rocket

In this guide

Varnish Add-on

You would probably know if your server uses Varnish; however, if you’re not sure, please ask your web host.

If your site is hosted on WP Engine, Flywheel or Cloudways, WP Rocket will automatically detect the environment and enable the Varnish add-on for you. On SiteGroundKinsta and Pressable you don’t need to enable this option either, because we already sync WP Rocket’s cache with their server cache.

On other hosting environments, if you know that your server uses Varnish, switch Add-on status to On for the Varnish add-on. That way, WP Rocket will make sure the Varnish cache is purged whenever the WP Rocket cache is purged.

Note: If you were using the Varnish HTTP Purge plugin before, you will no longer need it once you’ve switched on the Add-on.

Recommended Varnish configuration

We recommend that you use this guide to set up your Varnish configuration:
https://www.varnish-software.com/wiki/content/tutorials/wordpress/wp_step_by_step.html

WP Rocket's purge requests automatically follow this set-up. 

Please note, there's a small typo in Varnish configuration template included in the documentation:
ban("req.url ~ " + req.url + " && req.http.host ~ " + req.http.host); 
Should be
ban("req.url ~ " + req.url + " && req.http.host ~ " + req.http.host);

Custom Varnish IPs

Our Varnish integration assumes your server uses the default IP setting of 127.0.0.1. However, if you need to specify a different IP or multiple IPs, you can do that by installing this helper plugin:

📥   Download (.zip):  WP Rocket | Varnish IP
Developers: You can find the code for this plugin on GitHub.

Change purge method to BAN

WP Rocket's purge requests follow the recommended configuration from Varnish:
https://www.varnish-software.com/wiki/content/tutorials/wordpress/wp_step_by_step.html#handling-purge-requests

  • If the purge request contains the header X-purge-method: regex, it uses ban
  • If not, it uses a normal purge

However, if you are not able to modify your VCL configuration, you may use the below snippet to change the purge request method to BAN instead.

/**
 * Modify Varnish purge request from PURGE to BAN
 * 
 * @param $args array Array of arguments for the request.
 */
function mysite_wp_rocket_modify_varnish_purge_method ( $args ) {
	
	$args[ 'method' ] = 'BAN';
	
	return $args; 
}
add_filter( 'rocket_varnish_purge_request_args', 'mysite_wp_rocket_modify_varnish_purge_method' );

Change purge scheme to HTTPS

WP Rocket uses HTTP to send the purge request to Varnish. Depending on your Varnish configuration, you may need to switch the scheme to HTTPS. You can do that with the below snippet: 

/**
 * Change Varnish scheme from HTTP to HTTPS
 * 
 */

function wp_rocket_change_varnish_scheme ( $scheme ) {

	$scheme = 'https'	
	return $scheme;

}

add_filter( 'rocket_varnish_http_purge_scheme', 'mysite_wp_rocket_change_varnish_scheme' );

Using Varnish with a proxy

If you are using Varnish in conjunction with a proxy like Cloudflare, please see this guide.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.