Support » Plugin: Proxy Cache Purge » Supply Varnish IP via Filter

  • Resolved floatingio

    (@floatingio)


    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;
    }

    https://wordpress.org/plugins/varnish-http-purge/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    and the varnish instance is on a different IP per environment (which I’d rather not set via wordpress options).

    Why not use the alternative of a define?

    I’m assuming you’ve set up your wp-config.php in a way that it calls a different DB for each environment, right?

    Commonly it looks like:

    if ( ENV = Prod ) {
    define( 'DB_NAME', 'database_name_here' );
    define( 'DB_USER', 'username_here' );
    define( 'DB_PASSWORD', 'password_here' );
    define( 'DB_HOST', 'localhost' );
    } elseif ( ENV = dev) {
    ...
    }

    And so on.

    So define('VHP_VARNISH_IP','123.45.67.89'); goes in there too. Wouldn’t that also work?

    Thread Starter floatingio

    (@floatingio)

    I do a lot of environmental management via DNS (including things like database servers, varnish servers, etc.). This allows me to have a single configuration that works in all environments. Adding an environment is as simple as adding it to DNS; I don’t have to make updates in multiple places.

    There are, of course, exceptions — but my environment gets frequently deployed, and keeping the critical stuff like server locations in DNS ensures that fewer mistakes are made.

    I can also see a use for the filter in a sharding situation. If you have multiple varnish instances behind a load balancer, you could easily direct images to one instance, pages to another, posts to a third, or however you want to set it up. The filter would provide a simple mechanism for supporting this in VHP.

    Thread Starter floatingio

    (@floatingio)

    (Realized after I posted that the shard scenario won’t work without the postId being passed, which I don’t think is available where that filter is called. Oh well; it was a nice thought anyway. Maybe pass the URL to be purged?)

    Plugin Contributor Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Huh. I’ll put this in the 3.9 build.

    https://github.com/Ipstenu/varnish-http-purge/tree/REL_3.9

    Thread Starter floatingio

    (@floatingio)

    Thanks, much appreciated. 🙂

    Thread Starter floatingio

    (@floatingio)

    (blah. didn’t mean to double-post, and there’s no delete button.)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Supply Varnish IP via Filter’ is closed to new replies.