Apache-2.2

Varnish 4 不記憶體

  • May 13, 2015

我是使用清漆的新手。我安裝了它,我想我配置正確。為了測試這是我所做的:

我創建了一個只有字元串“test”的測試頁面。我去了頁面,它有這些標題:

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”。我相信如果記憶體正確,它應該顯示“測試”。

我沒有設置 cookie 或其他任何東西,僅此而已。我的 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 的機會很大。Varnish 不會記憶體任何帶有 cookie 的內容。這來自Varnish 4的 builtin.vcl :

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(針對特定路徑)

在這種情況下,我們刪除了預定義路徑下對象的 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 URL,您將不會發送任何 cookie,並且如果您在一秒後重複該操作,您應該會得到一個大於 0 的 Age 標頭。

引用自:https://serverfault.com/questions/691541