【发布时间】: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 的站点地图索引,其中包含其他站点地图(用于帖子、页面、作者等)。这也很好用。
- 问题是如何防止 Varnish 缓存我的站点地图?
- 如何防止 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