我在 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的新手,有人可以指导我如何修改配置。 :( 请帮忙。
更新:
如果我在
sub vcl_recv
中包含以下内容,它会起作用吗?if (req.url ~ "\.xml(\.gz)?$") {
return (pass);
}
最佳答案
请删除这些行!
if (req.url ~ "\.xml(\.gz)?$") {
return (pass);
}
返回(通过)是一种解决方法,但这不是您想要使用 Varnish 的方式。
Varnish 用于缓存页面和内容,例如
sitemap_index.xml
你已经在 VCL 中实现了 PURGE 机制,所以最简单的方法来处理你的
sitemap_index.xml
问题是清除它!基本原理是
sitemap_index.xml
只要没有发布新帖子,就需要缓存。然后,每次创建新帖子时,您都必须通知 Varnish sitemap_index.xml
通过发送下面的 HTTP 请求不再有效(从官方文档(1)粘贴):PURGE /sitemap_index.xml HTTP/1.0
Host: example.com
所以,我想你可以选择手动编辑你的模块或使用 Varnish HTTP Purge/WordPress 模块(也可能手动破解它)(2)
关于php - 如何阻止 Varnish 缓存站点地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20718681/