0

我有一个 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 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;
    }
}
4

1 回答 1

0

在这种情况下,我不认为 Varnish 是罪魁祸首,但是您的 VCL 对于 drupal 来说是非常不寻常的(例如, vcl_fetch 中的会话部分是错误的)。

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

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

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

我还建议您查看针对 drupal [2] [3] 进行战斗测试的 VCL

[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

于 2013-07-26T08:47:59.303 回答