Drupal 7和Varnish映像缓存设置src为127.0.0.1

时间:2013-07-24 14:21:19

标签: caching drupal drupal-7 varnish

我有一个Drupal 7安装,有时会将图像的src路径设置为127.0.0.1。 这是一个例子。

<img height="291" width="233" style="width: 233px; height: 291px; float: left;" class="media-image media-element file-default" typeof="foaf:Image" src="http://127.0.0.1/sites/default/files/media/news/images/jerzy_sawicki.jpg" alt="" title="">

清除缓存后,图像src暂时正常。

<img height="291" width="233" style="width: 233px; height: 291px; float: left;" class="media-image media-element file-default" typeof="foaf:Image" src="http://www.example.com/sites/default/files/media/news/images/jerzy_sawicki.jpg" alt="" title="">

我启用了很多贡献的模块,但我想这很可能是一个Varnish或Cache Expiration问题。

这是Varnish default.vcl配置。我已经将127.0.0.1更改为可能影响src的服务器名称,但它没有。

backend default {
  .host = "www.example.com";
  .port = "8888";
  .connect_timeout = 10s;
  .first_byte_timeout = 10s;
  .between_bytes_timeout = 10s;
  // Check Drupal every 5 minutes to keep cache warm.
  .probe = {
    .url = "/news";
    .interval = 300s;
    .timeout = 10s;
    .window = 5;
    .threshold = 2;
  }
}

sub vcl_recv {
  // Remove has_js and Google Analytics __* cookies.
  set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
  // Remove a ";" prefix, if present.
  set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  // Remove empty cookies.
  if (req.http.Cookie ~ "^\s*$") {
    unset req.http.Cookie;
  }

  // Catch Drupal theme files – THIS BREAKS UPDATE.PHP
  if (req.url ~ "^/sites/") {
    unset req.http.Cookie;
  }
  // Catch Drupal misc files (like drupal.js and jquery.js)
  if (req.url ~ "^/misc/") {
    unset req.http.Cookie;
  }

  // Drupal js/css doesn’t need cookies, cache them
  if (req.url ~ "^/modules/.*\.(js|css)\?") {
    unset req.http.Cookie;
  }

  // Pass cron jobs
  if (req.url ~ "cron.php" ||
    req.url ~ "^/admin/structure/features$" ||
    req.url ~ "^/admin/config/system/backup_migrate$") {
    return (pass);
  }
  // Currently we have server-status monitoring going directly against 8888 port
  // Commenting out this pass-through
  //if (req.url ~ ".*/server-status$") {
    //return (pass);
  //}

  # Add a unique header containing the client address
  remove req.http.X-Forwarded-For;
  set    req.http.X-Forwarded-For = client.ip;
}

sub vcl_hash {
  if (req.http.Cookie) {
    set req.hash += req.http.Cookie;
  }
}

sub vcl_deliver {
  if (obj.hits > 0) {
    set resp.http.X-Cache = "HIT";
  } else {
    set resp.http.X-Cache = "MISS";
  }
}

sub vcl_fetch {

    # Varnish determined the object was not cacheable
    if (!beresp.cacheable) {
        set beresp.http.X-Cacheable = "NO:Not Cacheable";

    # You don't wish to cache content for logged in users
    } elsif (req.http.Cookie ~ "(UserID|_session)") {
        set beresp.http.X-Cacheable = "NO:Got Session";
        return(pass);

    # You are respecting the Cache-Control=private header from the backend
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        return(pass);

    # You are extending the lifetime of the object artificially
    } elsif (beresp.ttl < 1s) {
        set beresp.ttl   = 5s;
        set beresp.grace = 5s;
        set beresp.http.X-Cacheable = "YES:FORCED";

    # Varnish determined the object was cacheable
    } else {
        set beresp.http.X-Cacheable = "YES";
    }

    # ....

    return(deliver);
sub vcl_error {
    # If 503 error and we've tried less than 3 times, try again
    if (obj.status == 503 && req.restarts < 3) {
        restart;
    }
}

1 个答案:

答案 0 :(得分:0)

我不认为Varnish在这种情况下应该受到责备,但你的VCL对drupal来说很不寻常(例如,vcl_fetch中的会话部分是错误的。)

另外,drupal应生成相对URL而不是绝对URL。

为了快速修复,我建议你在settings.php中设置$ base_url值[1]

$base_url = 'http://yourdomain.tld';

我还建议你去看看测试VCL的drupal [2] [3]

[1] https://api.drupal.org/api/drupal/developer!globals.php/global/base_url/7

[2] http://www.lullabot.com/blog/article/configuring-varnish-high-availability-multiple-web-servers     http://www.lullabot.com/sites/lullabot.com/files/default_varnish3.vcl_.txt

[3] https://github.com/NITEMAN/varnish-bites/blob/master/varnish3/drupal-base.vcl