4

I have been trying to understand why the expires header is set to the past. I read the comment In boostrap.inc drupal 6 but I am still a bit confused about it and the relation between expires and vary headers. I looked into this because I am trying to set the expire header in the hope of controlling varnish object cache expiration regardless of TTL. The code reads:

  // HTTP/1.0 proxies do not support the Vary header, so prevent any caching
  // by sending an Expires date in the past. HTTP/1.1 clients ignores the
  // Expires header if a Cache-Control: max-age= directive is specified (see RFC
  // 2616, section 14.9.3).
  $default_headers['Expires'] = 'Sun, 11 Mar 1984 12:00:00 GMT';

Would it affect me if I set the caching headers like expire? I want to do this only for anonymous users.

I posted a varnish question related to this here: https://stackoverflow.com/questions/19121220/how-to-control-how-long-varnish-expire-a-page-from-the-backend

4

See http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3.

If a response includes both an Expires header and a max-age directive, the max-age directive overrides the Expires header, even if the Expires header is more restrictive. This rule allows an origin server to provide, for a given response, a longer expiration time to an HTTP/1.1 (or later) cache than to an HTTP/1.0 cache.

And if you look at the following code, you can see that the reason they do that is to prevent caching and allow you to do your own caching. This disables the internal page cache but returns headers allowing downstream caches (such as Squid, Varnish, and other reverse proxies) to cache full pages.

function drupal_page_cache_header_external() {
  // Get headers set in hook_boot(). Keys are lower-case.
  $hook_boot_headers = drupal_get_header();

  $max_age = variable_get('page_cache_max_age', 0);
  drupal_set_header('Cache-Control', 'public, max-age=' . $max_age);
  drupal_set_header('Last-Modified', gmdate(DATE_RFC1123, $_SERVER['REQUEST_TIME']));

  // HTTP/1.0 proxies do not support the Vary header, so prevent any caching
  // by sending an Expires date in the past. HTTP/1.1 clients ignores the
  // Expires header if a Cache-Control: max-age= directive is specified (see RFC
  // 2616, section 14.9.3).
  drupal_set_header('Expires', 'Sun, 11 Mar 1984 12:00:00 GMT');

  // Allow HTTP proxies to cache pages for anonymous users without a session
  // cookie. The Vary header is used to indicates the set of request-header
  // fields that fully determines whether a cache is permitted to use the
  // response to reply to a subsequent request for a given URL without
  // revalidation. If a Vary header has been set in hook_boot(), it is assumed
  // that the module knows how to cache the page.
  if (!isset($hook_boot_headers['vary']) && !variable_get('omit_vary_cookie', FALSE)) {
    drupal_set_header('Vary', 'Cookie');
  }
}
9
  • 2
    The date also happens to be Dries' birthday IIRC.
    – Letharion
    Oct 2 '13 at 8:52
  • I did read this but I am still not clear. What happens if I change the expire header to a time in the future? 1. Does this mean it will only have effects if I have reverse proxies cache? 2. if I have varnish, and max-age is set = 0 and expire to sometime in future, will this have any affect? I also followed through this thread which I found helpful drupal.org/node/147310
    – awm
    Oct 2 '13 at 14:38
  • This stuff is a little hard to explain, there is a reason why some people's jobs is simply managing a caching system. All that that above does is allows you to use varnish to properly control caching and expiring. That function above only gets ran if pressflow is set to cache external. Otherwise it will do the normal stuff.
    – Steven
    Oct 2 '13 at 14:42
  • max-age will override the expire header irregardless of what the expire header says. If you set max-age to 0 and set the expire to sometime in the future. It will not cache anything.
    – Steven
    Oct 2 '13 at 14:43
  • Steven, anyone can modify headers using some hooks. You do not have to use external caching in order to add and modify headers. I am doing it myself and this is why I am digging into it. I read some rfc documentation about cache control and it is a little hard to explain but clearer than what is going on with drupal.
    – awm
    Oct 2 '13 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.