User Tools

Site Tools


gen:enable_varnish

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
gen:enable_varnish [2025/09/16 10:02] kirtisinghgen:enable_varnish [2025/11/08 05:55] (current) kirtisingh
Line 34: Line 34:
 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 . 
  
-This plugin might not work for the website root i.e. , when the home/front page of the WordPress website is made by collection of elements of other pages/blocks. In order to have seemless cache purge for the root or / page, we advise that you append the following block of code in the functions.php file of your WordPress theme . If the functions.php file does not exist, please create one.+**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.
  
-<code> +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. 
-/* action w.r.tsave post in order to flush URL - start */+ 
 +**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){ function custom2_save_post_action($post_id, $post, $update){
Line 83: Line 127:
 } }
  
-add_action( 'save_post', 'custom2_save_post_action', 10, );+add_action( 'upgrader_process_complete', 'custom2_varnish_flush_all', 10, );
  
-/* action w.r.t. save post in order to flush / URL - end */+add_action( 'save_post', 'custom2_save_post_action', 10, 3 );
 </code>   </code>  
  
gen/enable_varnish.1758016936.txt.gz · Last modified: 2025/09/16 10:02 by kirtisingh

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki