We have separate dns aliases for accessing the uncached backend use (for editors) and the cached backend. In these cases it's necessary to change the Varnish module to allow for a separate field on the admin form to specify a host name. This code provides a default (the php server name) and allows for manual editing.

varnish.admin.inc

$form['varnish_clear_host'] = array(
    '#type' => 'textfield',
    '#title' => t('Varnish Clear Host'),
    '#default_value' => variable_get('varnish_clear_host', $_SERVER['SERVER_NAME']),
    '#required' => TRUE,    
    '#description' => t('Set this to the server IP or hostname that you wish to use in purges'),
  );

varnish.module

function _varnish_get_host() {
  global $base_url;
  $parts = parse_url($base_url);
  if($host = variable_get('varnish_clear_host')){
    return $host;
  } else {
    return $parts['host'];
  }
}

Attached a patch for this as well.

Comments

David Stosik’s picture

Version: 6.x-1.1 » 7.x-1.x-dev

Hello,

Thanks for this patch that sounds great. I think it's not sufficient though, as the website may have many public cached domain names. See the issue I opened about it for more details: #1847672: VarnishCache::clear() has different behaviour than DrupalDatabaseCache::clear().. I think we need to discuss what would be the most appropriate thing to do when clearing all cache.
Should Varnish module gather a list of all accessed and cached domain names, to use them later when needing to clear cache?
What about Domain Access use case, where one would like to clear cache of all domain names related to only one domain?
We must not forget that a same Varnish instance may be used by completely different websites, which means we cannot clear all Varnish cache using just a wildcard.
I like what's done in Memcache, using a prefix key that allows to identify all cache coming from one site.

Let's discuss about it.

Thanks again,

David

PS: as this bug is still there in most recent 7.x dev version, I changed this issue version for more visibility.

cato’s picture

Hi David.

This is not a simple use-case as the variables of cache purging can span anything from hostnames to rewrites in the vcl.
This patch solves an immediate problem for my situation but may not apply to all. I have myself disabled the clearing of Varnish cache when Drupal cache is cleared because it is too expensive - i.e. it purges to greedily and the result is that everything is cleared from cache which is less than optimal.
The solution I have for this is to use Rules and Cache Actions to expire content selectively. As much of our content is served through views, I have set up Rules to run when content of specific types are updated and then configured Cache Actions to purge the Views path from the cache.

As for prefix keys, the hostname is our prefix in this case and thanks to Rules and Cache Actions we can also combine the prefix with a specific key (views path / content path) to purge only the matching content.

Domain access - I have no idea. I've yet to develop anything with DA.

I think that the Varnish module needs a little more sophistication in the way of configureable expires and things like support for purges based on content type, purges of views paths and a bit more fine-grained configuration overall.

szds’s picture

Thanks cato,
I happened to come upon a similar solution as cato's and was happy to notice a "great minds ..." situation :)

My solution has a minor variation though, that is, instead of the patch for the varnish.admin.inc I added a line to settings.php
$conf['varnish_host']='HOSTNAME';

In my case the change to _varnish_get_host() looks as follow where varnish_host = cato's varnish_clear_host

function _varnish_get_host() {
  global $base_url;
  $parts = parse_url($base_url);
  $varnish_host = variable_get('varnish_host',$parts['host']);
  return $varnish_host;
}

To address David's comment in #1, say the same varnish server is used for different websites, say
a.example.com
b.example.com
...
then the following line in the settings.php
$conf['varnish_host']='example.php$';

will issue a ban
ban req.http.host ~ example.com$ && req.url ~ /
when you "Flush all caches", thus banning all URLs of all hostnames that end with example.com

jan kellermann’s picture

Issue summary: View changes
StatusFileSize
new1.01 KB

I like the solution from szds in combination with admin-form from cato. see patch varnish-add-host-config-1812944-4.patch

kenorb’s picture

Status: Active » Needs review