User Tools

Site Tools


gen:enable_varnish

How to enable/use Varnish with a WordPress Website

Step 1 : Log into N99panel. From under N99panel dashboard, click 'VHosts/Websites'


Step 2 : You will see the list of all the VHosts. Please click 'Details/Manage' link against the appropriate VHost/Website


Step 3 : Scroll down to varnish and click the pencil icon against it. Current status of Varnish would also be shown over there


Step 4 : Now select the appropriate option for Varnish.If Varnish is already disabled, choose 'Enable - config 3'.

There are five options for Varnish :

- Disable
- Enable – Config 3
- Enable – Config 2
- Enable – Config 1
- Purge/Clear-cache

The internal IP of the Varnish server is 172.16.26.1

Please put this IP w.r.t. any software/plugin which is managing varnish cache

With WordPress you can easily use Varnish and enable auto-purging of content (when it changes), by using the Proxy Cache Purge .After installing the plugin. kindly make sure that you that this plugin points to varnish server at 172.16.26.1 .

Now if the page you changed/added is also referred/shown on the home page, Proxy Cache Purge plugin might not flush the cache for the website root i.e. / . In order to have seemless cache purge for the root or / page, we advise that you append the following block of code to the functions.php file of your WordPress theme . If the functions.php file does not exist, please create one.

/* action w.r.t. save post in order to flush / URL - start */
 
function custom2_save_post_action($post_id, $post, $update){
 
    if ( 'publish' !== $post->post_status ) {
	return;
    }
 
    preg_match('#^(/home/[^/]+/[^/]+)#',__DIR__,$matches);
 
    $vhost_dir=$matches[1];
 
    $hosts=array();
 
    $hosts[]=preg_replace('#^www\.#','',$_SERVER['HTTP_HOST']);
    $hosts[]='www.'.preg_replace('#^www\.#','',$_SERVER['HTTP_HOST']);
 
    for($i=0;$i<count($hosts);$i++){
 
        $ch = curl_init();
 
        curl_setopt($ch, CURLOPT_URL, 'http://172.16.26.1:6081/');
 
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PURGE");
 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Host: '.$hosts[$i]
        ));
 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
        $response = curl_exec($ch);
 
        if (curl_errno($ch)) {
            file_put_contents($vhost_dir.'/varnish_purge_via_php.log',date('Y-m-d H:i:s').' - '.$hosts[$i].' cURL error: ' . curl_error($ch)."\n",FILE_APPEND);    
        } else {
            file_put_contents($vhost_dir.'/varnish_purge_via_php.log',date("Y-m-d H:i:s").' - '.$hosts[$i].' handled'."\n",FILE_APPEND);
        }
 
        curl_close($ch);
 
    }
 
 
}
 
add_action( 'save_post', 'custom2_save_post_action', 10, 3 );
 
/* action w.r.t. save post in order to flush / URL - end */

In some scenarios auto-purging of Varnish cache may not happen even after using the Proxy Cache Purge plugin. Then in that case Purge/Clear-cache can be used to clear the cache. To disable Varnish, simply choose the Disable option.

When Varnish and specifically, when a higher number configuration is applied, it may impact certain sections of the website in unexpected ways, as certain cookies/sessions will have no effect based on varnish configuration selection. After applying a Varnish configuration, you should thoroughly check all parts of the website, and also ensure security and privacy is not compromised.

Important : When you are doing design/coding changes of your website, please disable Varnish in that case. Varnish cache or any cache for that matter should only be enabled in production ready mode.

Important : N99panel (Pack Web Hosting Pvt. Ltd.) is not responsible for erroneous or unintended consequences of Varnish implementaion

gen/enable_varnish.txt · Last modified: 2025/09/16 10:07 by kirtisingh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki