清漆configurationbuild议

我的VPS有几个问题。 我的主人不知道发生了什么事情,所以我不得不经历一场考验,尝试重新configuration一些东西。 其中一件事是清漆…我在这里发布了一个问题: https ://webmasters.stackexchange.com/questions/89506/http-header-if-not-modified-help/89507?noredirect =1# comment109928_89507我正在使用WP Super Cache来使用Mod_rewrite创build页面的静态HTML文件,以减less服务器负载。 然后,我通过清漆将这些静态页面进一步减less负载。

我遇到的问题是WP超级caching正在发送一个max-age = 3,这显然在3秒钟之后被logging为Varnish的另一个3秒钟。 所以真的,毫无意义的使用光油。

但是,如果将内容的最大年龄设置为更长,则意味着如果我在WordPress中更改了CSS或dynamic页面,则内容在浏览器中显得过时了,这显然是我不想要的。

我想知道(纠正我,如果这是不正确的路要走),如果按照这个指南: https: //www.varnish-cache.org/trac/wiki/VCLExampleLongerCaching光油可以去掉通过发送的头。 htaccess的WordPress,然后清漆caching一个星期(除非清除通过SSH或WP HTTP清除插件,我猜)和浏览器caching保持在15分钟低,所以如果有任何改变,它只会是15分钟的date,但在15分钟后,下一个请求仍然来自光油,而不是来自Apache。

我照顾的一些网站是摄影网站,所以我真的不想cachingGB的图像在Varnish所以我目前告诉我的VCL不要caching这些。 我只想caching页面,这样我就可以摆脱WordPress使用的额外caching插件。

我试图看看是否有一个方法来有一个长的清漆caching时间,然后WordPress会发送一个头,它将存储在浏览器的最大年龄说1天,但如果内容更新(网页或CSS文件为例),那么这将在浏览器中更新。 我似乎没有find答案,所以也许我要求太多。

这是我的VCL:

backend default { .host = "public IP"; .port = "8080"; } acl purge { "localhost"; "127.0.0.1";} sub vcl_recv { if (req.request == "PURGE") { if (!client.ip ~ purge) { error 405 "Not allowed."; } return (lookup); } #set req.grace = 60m; if (req.request != "GET" && req.request != "HEAD" && req.request != "PUT" && req.request != "POST" && req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") { return (pipe); } if (req.request != "GET" && req.request != "HEAD") { return (pass); } #if (req.http.Authorization || req.http.Cookie) { #return (pass); } return (lookup); # 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 and statcounter is_unique set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js|is_unique)=[^;]*", ""); # 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|cart|my-account|checkout|addons|administrator)") { # Don't cache, pass to backend return (pass); } if (req.url ~ "/administrator") { return (pass); } if ( req.url ~ "\?add-to-cart=" ) { return (pass); } if (req.url ~ "/(contact-us|contact|get-a-quote|upload-files|competition)") { return(pass); } # Never cache PUT, PATCH, DELETE or POST requests #if (req.method == "PUT" || req.method == "PATCH" || req.method == "DELETE" || req.method == "POST") { #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 ~ "lib/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 ~ "/lib/uploads/") { return (pass); } # any pages with captchas need to be excluded if (req.url ~ "^/contact/" || req.url ~ "^/links/domains-for-sale/") { 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 = 60m; } sub vcl_hit { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_miss { if (req.request == "PURGE") { purge; error 200 "Purged."; } } sub vcl_deliver { # multi-server webfarm? set a variable here so you can check # the headers to see which frontend served the request # set resp.http.X-Server = "server-01"; if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } } 

所以目前,清漆清理5分钟,但我的最大年龄在.htaccess的网站是3秒,这是毫无意义的。

我正在朝着正确的方向思考吗? 希望我有道理。 希望有人能让我看到光!

谢谢!

由于你使用了Varnish,所以我build议你在后端取出自定义的.htaccesscaching头文件,并从varnish中控制caching头文件。 Varnish网站上的VCLExampleLongerCaching示例应该为您正确执行此操作。

我发现curl对于向网站发出testing请求来查看caching头返回是非常有用的。 您可以执行此操作来查看针对给定请求返回的标头:

 curl -s -D - http://www.example.com -o /dev/null 

我试图看看是否有一个方法有一个长的清漆caching时间,然后WordPress会发送一个标题,它将存储在浏览器的最大年龄说1天,但如果内容更新(网页或CSS文件为例),那么这将在浏览器中更新。

这是一个非常普遍的要求,有多less网站处理它是通过添加一个独特的散列到页面源中的每个CSS / JS文件的URL的末尾。 每次文件更改时,唯一的哈希值也会发生变化,以便资产现在具有新的未caching的URL。 所以你只需要清除Varnish中的HTML页面,任何对引用资产的修改都会自动更新。 看来,Wordpress也使用这种技术 – 我只是看看一个随机的Wordpress网站,看到像“?v = 20150727”这样的datestring是在源文件中的每个CSS文件的末尾。