WP-Varnish and Domain mapping

Hi, we have a Multi Site install with some blogs using your wpmudev domain mapping plugin. WP-varnish is not purging on updates for these blogs.
I believe it's because they still have their original domain in wp-admin.

So, an ordinary blog uses url: blog1.myblog.tld = purge works
A custom blog uses url: mycustomblog.tld at the front-end but mycustomblog.myblog.tld in the back-end = purge does not work.

If I change the siteurl settings for the ones using mapped domains all images paths gets broken.

Hope I'm making sense :slight_smile:

http://wordpress.org/extend/plugins/wordpress-varnish/

  • Heydar
    • Flash Drive

    Hi Phil,

    Yes I tried change the settings for the domain mapping in network settings but if I do, the admin panel and/or images of the blogs with an custom domain gets broken.
    I have to have it set to the "original domain", i.e customdomain.site.tld

  • Heydar
    • Flash Drive

    Well, if I set it to "the mapped domain" or "the domain entered by the user":
    I cannot login, just gets redirected to the login splash again for those with their own domains :slight_frown:
    And this is even though I'm logged in as superuser
    I'm confused :wink:

  • Heydar
    • Flash Drive

    Sorry Phil, suddenly it seems to work logging in (perhaps cookie issue).
    Still, what needs to be changed aswell is the "domain" column of the wp_blogs table for the mapped domain blogs. They are still left default and wp-varnish is using that value I believe.

    So, even though I change the domain mapping settings to be "the mapped domain" or "the domain entered by the user", the value for domain in the wp_blogs is still set to the original domain.
    But, If I change it in the DB...it will mess up image url's

    hmm, am I making any sense :slight_smile:

  • Barry
    • DEV MAN’s Mascot

    So, even though I change the domain mapping settings to be "the mapped domain" or "the domain entered by the user", the value for domain in the wp_blogs is still set to the original domain.
    But, If I change it in the DB...it will mess up image url's

    HIya, It's domain mapping - not domain changing, changing the entries in the db (either manually or via a plugin) can cause a lot of hurt. The plugin overrides WP's access to the tables / options / settings so it thinks these values have changed.

  • Heydar
    • Flash Drive

    Ok, I have to have the domain mapping setting set to either "the original domain" or the "the domain added by the user".
    If I set it to "the mapped domain" they are unable to login. Just gets relooped back to the login screen.

    If I have it set to "domain added by the user", login and then change the url in the browser from

    http://customdomain.root.tld/wp-admin/
    to
    http://customdomain.tld/wp-admin/

    it will purge varnish on making a new post.

    Strange...what's the difference between "The domain added by the user" and "The mapped domain"?

  • Ajax McKerral
    • Design Lord, Child of Thor

    Hi - this is getting to be a very long thread - and I must admit that I haven't read it all. However I do have a solution for the Domain Mapping and Varnish Purge plugin issues. The solution is to query the database table wp_domain_mapping from within the purging class. Get all mapped domains for the site, and put them in an array, then loop through the array running the purge comands.

    From (approximately) line 61 of varnish-purger-abstract.class.php insert the following code up to where you see the closing bracket before the line that says:
    function WPAuth($challenge, $secret) {

    Paste this code over the existing lines:
    (Note that wpv_host has become an array wpv_hosts)

    $wpv_wpurl = get_bloginfo('wpurl');
        $wpv_replace_wpurl = '/^http:\/\/([^\/]+)(.*)/i';
        $wpv_hosts[] = preg_replace($wpv_replace_wpurl, "$1", $wpv_wpurl);
        $wpv_blogaddr = preg_replace($wpv_replace_wpurl, "$2", $wpv_wpurl);
        $wpv_url = $wpv_blogaddr . $wpv_url;
    
    	/*
    	* WPMU Domain mapping edit by ajax@nuttify.com
    	*--------------------------------------------------
    	*/
    
    			//Are we a subdomain install?
    			global $switched;
    			switch_to_blog(1);
    			$tldomain = get_bloginfo('wpurl');
    			restore_current_blog();
    			$tld_host = preg_replace($wpv_replace_wpurl, "$1", $tldomain);
    			preg_match("/$tld_host/","$wpv_hosts[0]",$matches);
    
    			if ($matches) :
    				global $wpdb ;
    				$domains = $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_domain_mapping WHERE blog_id = %d", $wpdb->blogid) );
    				if ( is_array( $domains ) && !empty( $domains ) ) :
    					foreach( $domains as $details ) :
    						$wpv_hosts[] =$details->domain ;
    					endforeach;
    				endif;
    				reset($domains);
    			endif; // if ($matches)
    	if ( is_array( $wpv_hosts ) && !empty( $wpv_hosts ) ) :
    		foreach ( $wpv_hosts as $wpv_host ) :
    			for ($i = 0; $i < count ($wpv_purgeaddr); $i++) {
    			  $varnish_sock = fsockopen($wpv_purgeaddr[$i], $wpv_purgeport[$i], $errno, $errstr, $wpv_timeout);
    			  if (!$varnish_sock) {
    				error_log("wp-varnish error: $errstr ($errno)");
    				return;
    			  }
    
    			  if($wpv_use_adminport) {
    				$buf = fread($varnish_sock, 1024);
    				if(preg_match('/(\w+)\s+Authentication required./', $buf, &$matches)) {
    					 # get the secret
    					 $secret = "1beb871d-987a-4bbd-98aa-408e3de596cb";
    					 fwrite($varnish_sock, "auth " . $this->WPAuth($matches[1], $secret) . "\n");
    					 $buf = fread($varnish_sock, 1024);
    					 if(!preg_match('/^200/', $buf)) {
    					   error_log("wp-varnish error: authentication failed using admin port");
    					  fclose($varnish_sock);
    					  return;
    					}
    				}
    				$out = "purge req.url ~ ^$wpv_url && req.http.host == $wpv_host\n";
    			  } else {
    				$out = "PURGE $wpv_url HTTP/1.0\r\n";
    				$out .= "Host: $wpv_host\r\n";
    				if ($useragent != null){
    				   $out .= "User-Agent: $useragent\r\n";
    				}
    				$out .= "Connection: Close\r\n\r\n";
    			  }
    			  fwrite($varnish_sock, $out);
    			  fclose($varnish_sock);
    			}
    			// store url as purged
    			   $WPVarnishPurger_url_purged[]=$wpv_url.$useragent;
    		endforeach ;
    	endif ;
    
    	/*
    	* End WPMU Domain mapping edit
    	*--------------------------------------------------
    	*/

    After doing this and uploading it to my server I then tested on a few sites on my multi-site WP install with mapped domains. Updating a post from within the subdomain backend works to clear the mapped domain as well.

    Using the varnishlog command with a regex for purges, returns:

    $ varnishlog -I "purge"
        0 CLI          - Rd purge req.url ~ ^/submit-your-meeting-experiences/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/submit-your-meeting-experiences/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/feed/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/feed/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/feed/atom/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/feed/atom/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/page/(.*) && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/page/(.*) && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/11/26/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/11/26/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/11/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/11/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2011/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/26/painful/9-autosave/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/26/painful/9-autosave/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/01/26/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/01/26/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/01/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/01/ && req.http.host == deathtomeetings.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/ && req.http.host == deathtomeetings.brellabee.com
        0 CLI          - Rd purge req.url ~ ^/date/2012/ && req.http.host == deathtomeetings.com

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.