2012-12-14 37 views
0

好的,所以我試圖讓Varnish正常工作,並且我有一個奇怪的行爲,我無法解釋(可能是由於缺乏理解)。爲什麼將Cookie添加到Varnish中的請求時,beresp.ttl設置爲0?

這裏是我想要做的事:

GET http://scm.dev:6081/articles 
If-None-Match: "50a0794aaca51fce202e86da428b7dca9b4bbff93" 

我也得到:

HTTP/1.1 200 OK 
Age: 3         // Correctly using a cached result 
Content-Length: 3878 
Content-Type: application/json 
Date: Fri, 14 Dec 2012 10:59:34 GMT 
Via: 1.1 varnish 
X-Cacheable: YES       // It is correct 
Cache-Control: max-age=10, public, s-maxage=20 
Etag: "50a0794aca51fce202e86da428b7dca9b4bbff93" 
Vary: Accept,Accept-Encoding 

現在,如果我添加一個Cookie我的要求:

GET http://scm.dev:6081/articles 
If-None-Match: "50a0794aaca51fce202e86da428b7dca9b4bbff93" 
Cookie: foo=bar 

我得到了非緩存響應和非緩存警告:

HTTP/1.1 200 OK 
Age: 0         // Always set to 0 
Content-Length: 3878 
Content-Type: application/json 
Date: Fri, 14 Dec 2012 10:59:34 GMT 
Via: 1.1 varnish 
X-Cacheable: NO:Not Cacheable    // It is not correct 
Cache-Control: max-age=10, public, s-maxage=20 
Etag: "50a0794aca51fce202e86da428b7dca9b4bbff93" 
Vary: Accept,Accept-Encoding 

如果檢查後VCL配置,如果beresp.ttl小於或等於0

爲什麼X-Cacheable: NO:Not Cacheable頭設置?

我還以爲有Cookie請求返回一個Cache-Control: public應儘可能後端緩存承擔責任,正確設置參數public當它應該是。

我本來以爲X-Cacheable: NO:Got Session在使用Cookie進行請求時已經設置好了,但它甚至沒有更進一步。

的命令行啓動清漆守護:

/usr/sbin/varnishd -P /var/run/varnishd.pid -a :6081 -T 192.168.1.100:6082 -f /etc/varnish/custom.vcl -S /etc/varnish/secret -s malloc,256m

這裏是我的custom.vcl

backend scm { 
.host = "scm.dev"; 
.port = "80"; 
} 

sub vcl_recv { 
    set req.http.Surrogate-Capability = "abc=ESI/1.0"; 
} 

sub vcl_fetch { 
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") { 
     unset beresp.http.Surrogate-Control; 
     set beresp.do_esi = true; 
    } 

    # Varnish determined the object was not cacheable 
    if (beresp.ttl <= 0s) { 
     set beresp.http.X-Cacheable = "NO:Not Cacheable"; 

    # You don't wish to cache content for logged in users 
    } elsif (req.http.Cookie ~ "(foo)") { 
     set beresp.http.X-Cacheable = "NO:Got Session"; 

     return(hit_for_pass); 
    # You are respecting the Cache-Control=private header from the backend 
    } elsif (beresp.http.Cache-Control ~ "private") { 
     set beresp.http.X-Cacheable = "NO:Cache-Control=private"; 

     return(hit_for_pass); 
    # Varnish determined the object was cacheable 
    } else { 
     set beresp.http.X-Cacheable = "YES"; 
    } 

    return(deliver); 
} 

sub vcl_hit { 
    if (req.request == "PURGE") { 
     set obj.ttl = 0s; 
     error 200 "Purged"; 
    } 
} 

sub vcl_miss { 
    if (req.request == "PURGE") { 
     error 404 "Not purged"; 
    } 
} 

回答

1

光油不會,by design,獲取從緩存中的任何請求,如果餅乾都存在。 Cookie通常表示響應對於特定用戶是唯一的。

您可以unset req.http.Cookie;vcl_recv()完全,如果你不關心餅乾,或者如果你想保持特定的Cookie做

if(req.http.Cookie) { 
    if (req.http.Cookie !~ "(important_cookie|php_session)") { 
    remove req.http.Cookie; 
    } 
} 
+0

以及我在我的問題說,我認爲有一個請求Cookie,但通過「Cache-Control:public」返回響應應該由Varnish緩存。聽起來不正確嗎?如果是這樣,並且如果我理解正確,我必須自己實現這種行爲,對吧? –

+0

不幸的是沒有。 Varnish只查看請求標題並根據它們決定傳遞請求。即它不會先查找緩存副本,而是它的標頭,但會繞過緩存。總是。如果您知道可以安全地緩存某些標題的內容,則可以將該Cookie添加到散列中。 https://www.varnish-cache.org/trac/wiki/VCLExampleCacheCookies – Ketola

相關問題