清漆4不caching

我是新的与清漆工作。 我安装了它,我认为我configuration正确。 为了testing这是我做了什么:

我创build了一个只有string“testing”的testing页面。 我去了页面,它有这些标题:

Accept-Ranges:bytes Age:0 Cache-Control:max-age=120 Connection:keep-alive Content-Length:6 Content-Type:text/html; charset=UTF-8 Date:Tue, 12 May 2015 19:35:34 GMT Expires:Tue, 12 May 2015 19:37:34 GMT Server:Apache/2.2.15 (CentOS) Via:1.1 varnish-v4 X-Powered-By:PHP/5.3.3 X-Varnish:32829 

我将文件中的文本更改为“test2”我去了页面,它显示“test2”。 我相信它应该显示“testing”,如果它正确caching。

我没有设置cookies或任何东西,只是。 我的vcl很简单:

 vcl 4.0; # Default backend definition. Set this to point to your content server. backend default { .host = "127.0.0.1"; .port = "8080"; } sub vcl_recv { # Happens before we check if we have this in cache already. # # Typically you clean up the request here, removing cookies you don't need, # rewriting the request, etc. } sub vcl_backend_response { # Happens after we have read the response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. #This will set up "grace mode". set beresp.ttl = 10s; set beresp.grace = 1h; } sub vcl_deliver { # Happens when we have all the pieces we need, and are about to send the # response to the client. # # You can do accounting or modifying the final object here. } 

任何想法? 谢谢

在您的请求中发送cookie的机会很大。 光油不会在其中caching任何cookie。 这来自builtin.vcl for Varnish 4 :

 47 sub vcl_recv { 48 if (req.method == "PRI") { 49 /* We do not support SPDY or HTTP/2.0 */ 50 return (synth(405)); 51 } 52 if (req.method != "GET" && 53 req.method != "HEAD" && 54 req.method != "PUT" && 55 req.method != "POST" && 56 req.method != "TRACE" && 57 req.method != "OPTIONS" && 58 req.method != "DELETE") { 59 /* Non-RFC2616 or CONNECT which is weird. */ 60 return (pipe); 61 } 62 63 if (req.method != "GET" && req.method != "HEAD") { 64 /* We only deal with GET and HEAD by default */ 65 return (pass); 66 } 67 if (req.http.Authorization || req.http.Cookie) { 68 /* Not cacheable by default */ 69 return (pass); 70 } 71 return (hash); 72 } 

您需要从您的VCL中删除不需要的Cookie,如Varnish网站中的示例所示

从后端删除Set-Cookie(对于特定的path)

在这种情况下,我们删除预定义path下的对象的Cookie头和Set-Cookie头。 对于图像和类似的静态内容,这是相当普遍的。

 sub vcl_recv { if (req.url ~ "^/images") { unset req.http.cookie; } } sub vcl_backend_response { if (req.url ~ "^/images") { unset beresp.http.set-cookie; } } 

如果您使用curl -i URLtesting,则不会发送任何cookie,如果您稍后重复一次,则应该获得大于0的Age标头。

我不确定您是否正确设置了宽限模式。 清漆书似乎表明,你应该在vcl_fetchvcl_recv设置一些。

(我不熟悉Varnish,但我希望很快)。

核心宽限机制

渐变对象是已过期的对象,但仍保留在caching中宽限模式是当Varnish使用渐变对象时使用渐变对象的方法不止一种。 req.grace定义了一个对象可以用多长时间来让Varnish仍然认为是宽限模式。 beresp.grace定义了过去beresp.ttl-time Varnish会保留一个对象的时间。req.grace通常在vcl_recv中根据后端的状态进行修改。 当Varnish处于宽限模式时,就TTL而言,它使用已经过期的对象。 有几个原因可能发生,其中之一就是如果后端被健康探测器标记为不良。 为了清漆能够使用一个增光的物体,有两件事情需要发生:

该对象还需要保持在周围。 这受vcl_fetch中的beresp.grace影响。 VCL必须让Varnish像过去那样使用过期的物体。 这受vcl_recv中的req.grace影响。 设置宽限时,您将需要修改vcl_recv和vcl_fetch以有效使用宽限。 使用宽限的典型方法是将对象存储在TTL之后几个小时,但只能在TTL后的几秒钟内使用它,除非后端生病。 我们稍后会更多关注健康检查,但现在,下面的VCL可以说明一个正常的设置:

 sub vcl_recv { if (req.backend.healthy) { set req.grace = 30s; } else { set req.grace = 24h; } } sub vcl_fetch { set beresp.grace = 24h; } 

检查Apache发送什么头文件到Varnish。 您也可以在Varnish中明确设置内容的TTL。

你可以尝试增加ttl值beresp.ttl = 30m; 我认为清漆具有120秒的默认ttl值。

年龄:标题的价值在每下一个命中增加?