User Tools

Site Tools


gen:enable_varnish

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
gen:enable_varnish [2024/04/02 03:21] – created kirtisinghgen:enable_varnish [2025/11/08 05:55] (current) kirtisingh
Line 32: Line 32:
 Please put this IP w.r.t. any software/plugin which is managing varnish cache 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 [[https://wordpress.org/plugins/varnish-http-purge/|Proxy Cache Purge]] .After installing the plugin. kindly make sure that you that this plugin points to varnish server at 172.16.26.1 +With WordPress you can easily use Varnish and enable auto-purging of content (when it changes), by using the [[https://wordpress.org/plugins/varnish-http-purge/|Proxy Cache Purge]] .After installing the plugin. kindly make sure that you that this plugin points to varnish server at 172.16.26.1 .  
 + 
 +**In some scenarios, Proxy Cache Purge plugin might not flush the cache or may not be of help**\\  
 +- If the page you changed/added is also referred/shown on the home page\\ 
 +- If a plugin/theme is updated which is related to visual aspects of your website. 
 + 
 +For those cases 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. 
 + 
 +**Important : Please check verify the functionality after implementing it. This code snippet is provieded only as a goodwill gesture** 
 + 
 +<code php> 
 +function custom2_varnish_flush_all($upgrader_object, $options){ 
 + 
 +    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], 
 +     'X-Purge-Method: regex' 
 +        )); 
 + 
 +        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); 
 + 
 +    } 
 + 
 +
 + 
 +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( 'upgrader_process_complete', 'custom2_varnish_flush_all', 10, 2 ); 
 + 
 +add_action( 'save_post', 'custom2_save_post_action', 10, 3 ); 
 +</code>  
  
 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.  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. 
gen/enable_varnish.1712028072.txt.gz · Last modified: 2024/04/02 03:21 by kirtisingh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki