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/09/16 10:07] (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 .  
 + 
 +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. 
 + 
 +<code php> 
 +/* 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 */ 
 +</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