I have set up 3 subdomains, with admin all works fine. I flushed the cache.
When anonymous user visits one subdomain (and this one is loaded to the cache) this subdomain is displayed for all the other subdomain url requests till I flush the cache again. Then again, a different subdomain is always shown..

It seems that the caching doesn't take into account the subdomain in url and always displays the lastly cached front page... When I turn off caching, everything work ok.

any ideas what I could do about it?

Comments

agentrickard’s picture

What form of caching are you using? Drupal core's {cache_page} table uses the absolute URI as a cache ID, which solves this problem cleanly (we even have tests for it in the Drupal 7 code).

So there are a few things to check:

1) If using alternative caching, how is the cache id being set?

2) Is the domain request actually what you think it is? (Use the provided Server information block to test.) It may be that you have a server configuration issue that is returning the "wrong" domain.

brechacik’s picture

Status: Active » Closed (works as designed)

Thank you for your comment. I had disabled caching for a while, and I now that I have enabled it and checked it again – it started to work ok.

But then I made some changes in the administration (updated content), flushed the cache and the problem appeared again..

I'm not aware of using an alternative caching – it should be the default configuration.

The domain request seems ok.

brechacik’s picture

Status: Closed (works as designed) » Active

So I looked at the database: I'm using the cache_page table. All entries that are being cached are of the main domain. It seems that the content of the subdomains is stored under the id of the main domain.

agentrickard’s picture

That is not normally true and may indicate a problem with the DNS request handling, if the HTTP_HOST value is not set as you expect.

In the {cache_page} table, the cid (the cache key) is an absolute URL, which is why DA works with core caching. Note also that you may disable caching for some domains if you are using Domain Conf. Check the settings at admin/build/domain/batch/cache.

This works fine normally and has for years, so there is something amiss in your configuration, so the question is where in the stack the error occurs.

agentrickard’s picture

Issue summary: View changes

information added

craigmc’s picture

All--
I just did some digging into this today and I think the issue comes up when you set a base_url in the settings.php

the page cache uses global $base_root . request_uri() as the page cache ID
drupal_page_get_cache
bootstrap.inc line 1055

$cache = cache_get($base_root . request_uri(), 'cache_page');

However if base_url is set in settings.php, this trumps what would be used for the base_root (e.g. the subdomain)
From bootstrap.inc line 746

  if (isset($base_url)) {
    // Parse fixed base URL from settings.php.
    $parts = parse_url($base_url);
    if (!isset($parts['path'])) {
      $parts['path'] = '';
    }
    $base_path = $parts['path'] . '/';
    // Build $base_root (everything until first slash after "scheme://").
    $base_root = substr($base_url, 0, strlen($base_url) - strlen($parts['path']));
  }

I haven't figured out an acceptable solution for this, but at this point, it looks like overriding and extending the base cache class for cache_page and forcing the actual requested instead of what is specified in $base_url/$base_root is going to be my path forward.

craigmc’s picture

And yes, overriding the base class and having a custom class handler for page cache worked out great for me.

in settings.php
// Add alternate handler for Page Cache
$conf['cache_backends'][] = 'sites/all/modules/custom/alt_page_cache/pageCacheRedis.inc';
$conf['cache_class_cache_page'] = 'pageCacheRedis';

in 'sites/all/modules/custom/alt_page_cache/pageCacheRedis.inc';


/**
 * @file
 * Wrapper class to handle page cache CID overrides for SG
 */
class pageCacheRedis extends Redis_Cache {
  function get($cid) {
    return parent::get(self::addExtraCidInfo($cid));
  }

  function getMultiple(&$cids) {
    foreach ($cids as $index => $cid) {
      $cids[$index] = self::addExtraCidInfo($cid);
    }
    return parent::getMultiple($cids);
  }

  function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {
    $cid = self::addExtraCidInfo($cid);
    parent::set($cid, $data, $expire, $headers);
  }

  static function addExtraCidInfo($cid) {
    return $cid . $_SERVER['HTTP_HOST'];
  }
}
agentrickard’s picture

Normally, you should not be hardcoding $base_url. That breaks how Domain Access works and is clearly documented.

agentrickard’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)
hugronaphor’s picture

In case you still need for some reason to hardcore your $base_url in settings.php here is a snippet you can use to avoid caching problems:

if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $base_url = 'https://' . $_SERVER['SERVER_NAME'];
}
else {
  $base_url = 'http://' . $_SERVER['SERVER_NAME'];
}