【问题标题】:How to stop Varnish from caching Sitemap?如何阻止 Varnish 缓存站点地图?
【发布时间】:2013-12-21 11:51:43
【问题描述】:

我在 Nginx 和 Varnish 上运行一个 Wordpress 博客。我正在为 Varnish 使用以下配置:

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# 
# Default backend definition.  Set this to point to your content
# server.
# 
backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
    .max_connections = 800;
}


acl purge {
        "localhost";
}

sub vcl_recv {
    set req.grace = 2m;

  # Set X-Forwarded-For header for logging in nginx
  remove req.http.X-Forwarded-For;
  set    req.http.X-Forwarded-For = client.ip;


  # Remove has_js and CloudFlare/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*", "");



# Either the admin pages or the login
if (req.url ~ "/wp-(login|admin|cron)") {
        # Don't cache, pass to backend
        return (pass);
}


# Remove the wp-settings-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-1=[^;]+(; )?", "");

# Remove the wp-settings-time-1 cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wp-settings-time-1=[^;]+(; )?", "");

# Remove the wp test cookie
set req.http.Cookie = regsuball(req.http.Cookie, "wordpress_test_cookie=[^;]+(; )?", "");

# Static content unique to the theme can be cached (so no user uploaded images)
# The reason I don't take the wp-content/uploads is because of cache size on bigger blogs
# that would fill up with all those files getting pushed into cache
if (req.url ~ "wp-content/themes/" && req.url ~ "\.(css|js|png|gif|jp(e)?g)") {
    unset req.http.cookie;
}

# Even if no cookies are present, I don't want my "uploads" to be cached due to their potential size
if (req.url ~ "/wp-content/uploads/") {
    return (pass);
}

# Check the cookies for wordpress-specific items
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") {
        # A wordpress specific cookie has been set
    return (pass);
}



    # allow PURGE from localhost
    if (req.request == "PURGE") {
        if (!client.ip ~ purge) {
            error 405 "Not allowed.";
        }
        return (lookup);
    }


    # Force lookup if the request is a no-cache request from the client
    if (req.http.Cache-Control ~ "no-cache") {
        return (pass);
    }


# Try a cache-lookup
return (lookup);

}

sub vcl_fetch {
    #set obj.grace = 5m;
    set beresp.grace = 2m;

}

sub vcl_hit {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

sub vcl_miss {
        if (req.request == "PURGE") {
                purge;
                error 200 "Purged.";
        }
}

我已经按照教程mentioned here

一切正常,但我使用Yoast SEO Plugin 在每个新帖子后动态生成站点地图。它生成一个名为sitemap_index.xml 的站点地图索引,其中包含其他站点地图(用于帖子、页面、作者等)。这也很好用。

  1. 问题是如何防止 Varnish 缓存我的站点地图?
  2. 如何防止 Varnish 干扰 Google Analytics?这不应该阻止 GA 向我提供正确的报告。

我是 Varnish 的新手,有人可以指导我如何修改配置。 :(请帮忙。

更新:

如果我在sub vcl_recv 中包含以下内容,它会起作用吗

if (req.url ~ "\.xml(\.gz)?$") {
   return (pass);
}

【问题讨论】:

  • 您是否尝试过您的站点地图示例?它应该工作。你应该把它放在返回(查找)之上;
  • 我已经更新了文件。我们现在还没有发布任何消息。我将不得不等待并监视它的变化。顺便说一句,Google Analytics 的配置是否正确?会对GA有什么不好的影响吗?我需要改变什么吗?
  • 不,您不会对 GA 产生任何影响。 GA 在客户端站点上执行(它是 javascript)。当您在 Varnish 配置中取消设置 cookie 时 - 您的 WordPress 站点不会看到它们,但浏览器和 GA 会。您可以在 FireBug 中检查 wp-settings-1 等所有 cookie 是否正常,但如果您尝试从 PHP 端获取此 cookie,您将看不到它们。 Varnish 将取消设置它们。

标签: php wordpress nginx sitemap varnish


【解决方案1】:

删除这些行!!

if (req.url ~ "\.xml(\.gz)?$") {
   return (pass);
}

返回(通过) 是一种解决方法,但这不是您想要使用 Varnish 的方式。 Varnish 用于缓存页面和内容,例如 sitemap_index.xml

您已经在 VCL 中实现了 PURGE 机制,因此处理 sitemap_index.xml 问题的最简单方法是 PURGE!

基本原则是sitemap_index.xml只要没有发新帖就需要缓存。然后,每次创建新帖子时,您必须通过发送下面的 HTTP 请求通知 Varnish sitemap_index.xml 不再有效(粘贴自官方文档(1)):

PURGE /sitemap_index.xml HTTP/1.0
Host: example.com

所以,我想您可以选择手动编辑模块或使用 Varnish HTTP Purge / WordPress 模块(也可能手动破解)(2)

  1. https://www.varnish-cache.org/docs/3.0/tutorial/purging.html#http-purges

  2. http://wordpress.org/plugins/varnish-http-purge/

【讨论】:

    【解决方案2】:

    如果我在 sub vcl_recv 中包含以下内容,它会起作用

    if (req.url ~ ".xml(.gz)?$") { 返回(通过); }

    这会奏效。将其放置在函数顶部附近。但请记住,它会阻止缓存 all .xml 文件和 all .xml.gz 文件。当然,您可能提供的大多数 xml 文件和 xml.gz 文件、站点地图,仍然是一个考虑因素,以防它们不是。

    【讨论】:

      【解决方案3】:

      我不能给你确切的语法,但你应该通过管道*站点地图的请求。

      *pipe - 匹配 vcl 中的请求并始终引导它从服务器获取它。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-05
      • 2012-06-25
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2018-01-09
      相关资源
      最近更新 更多