Using Varnish with WP Rocket

You can use WP Rocket and Varnish caching at the same time. In this article, you can learn how you can synchronize them using our Varnish Add-on, how to customize the synchronization, and other useful information.

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, Cloudways or WPX, 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.

Mobile Cache

By default, WP Rocket's Mobile Cache option creates a separate set of cache files for mobile devices. With this approach, any mobile-specific content will be cached and served independently. 

You must make sure that your Varnish configuration is set up to distinguish mobile requests from desktop requests. Otherwise, the wrong version of the cache could be served.

WP Rocket versions older than 3.16

If you want to use the Separate cache files for mobile devices sub-option, you also have to make sure that your Varnish configuration is set up to distinguish mobile from desktop devices.

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/developers/tutorials/purge/

  • 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.

Varnish Control Key

Unfortunately, WP Rocket is not compatible with the Varnish Control Key. 

An alternative, if you must use this key, is to use the Purge Varnish Cache plugin. Please note, WP Rocket's cache won't be synchronized with Varnish cache, so you'll need to clear the Varnish's cache after making changes to the website.

Known issues

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