Supply Varnish IP via Filter
-
I have several environments that I run wordpress in, and the varnish instance is on a different IP per environment (which I’d rather not set via wordpress options). To handle this, I’ve added a varnish-purge CNAME to DNS. Initially I set up VHP_VARNISH_IP at start time, but that requires the DNS lookup for every hit, even if a purge isn’t needed.
To solve this, I applied the following patch:
diff --git a/blog/wp-content/plugins/varnish-http-purge/varnish-http-purge.php b/blog/wp-content/plugins/varnish-http-purge/va index 284149b..eb9dc8c 100644 --- a/blog/wp-content/plugins/varnish-http-purge/varnish-http-purge.php +++ b/blog/wp-content/plugins/varnish-http-purge/varnish-http-purge.php @@ -233,6 +233,7 @@ class VarnishPurger { } else { $varniship = get_option('vhp_varnish_ip'); } + $varniship = apply_filters('vhp_varnish_ip', $varniship); if (isset($p['path'] ) ) { $path = $p['path'];
This allows me to supply a filter that can look up the varnish IP once, and then only if a purge is requested (which is why this is done in the purge code rather than at initialization).
I’d love to see this in the next version, so please feel free to steal it. 🙂 I’m sure other folks could do other interesting things with this.
For reference, my current (overly simple) filter looks like this:
add_filter('vhp_varnish_ip', fio_get_varnish_ip); function fio_get_varnish_ip($ip) { global $fio_varnish_ip; if (!$fio_varnish_ip) { $ip = dns_get_record('varnish-purge', DNS_ANY); if ($ip[0]['type'] == 'CNAME') { $ip = dns_get_record($ip[0]['target'], DNS_A); } if ($ip && $ip[0]['type'] == 'A') { $fio_varnish_ip = $ip[0]['ip']; } } if ($fio_varnish_ip) return $fio_varnish_ip; else return $ip; }
- The topic ‘Supply Varnish IP via Filter’ is closed to new replies.