簡體   English   中英

清漆:忽略一些用於緩存哈希計算的緩存Cookie

[英]Varnish: ignore some cache cookies for hash computation

情況::

  1. 即使在請求中存在Cookie,Varnish也需要緩存。
  2. 該請求可能包含N個任意cookie,其中某些已知cookie一定不能構成緩存鍵的一部分。 任意cookie不包含任何用戶敏感數據,例如。 他們是美容助手,例如is_authenticated = 1。
  3. 實際的后端必須接收未緩存的原始Cookie組,以防發生高速緩存未命中的情況。
  4. 我不想檢查VCL中的URL模式,因為這假定了對后端的了解過多。

令人驚訝的是,這很難解決。 到目前為止,我發現的所有解決方案均假定(2)為白名單,而我需要黑名單。 而且大多數解決方案都會刪除應該通過后端的Cookie。

那么(未經測試)如何:

# We use builtin.vcl logic and 'return' from our vcl_recv in order 
# to prevent default Varnish behaviour of not caching with cookies present
sub vcl_recv {
    # your vcl_recv starts here
    # ...
    # your vcl_recv ends here

    if (req.method == "PRI") {
    /* We do not support SPDY or HTTP/2.0 */
        return (synth(405));
    }
    if (req.method != "GET" &&
      req.method != "HEAD" &&
      req.method != "PUT" &&
      req.method != "POST" &&
      req.method != "TRACE" &&
      req.method != "OPTIONS" &&
      req.method != "DELETE") {
        /* Non-RFC2616 or CONNECT which is weird. */
        return (pipe);
    }

    if (req.method != "GET" && req.method != "HEAD") {
        /* We only deal with GET and HEAD by default */
        return (pass);
    }
    if (req.http.Authorization) {
        /* Not cacheable by default */
        return (pass);
    }
    return (hash);
}

sub vcl_hash {
    set req.http.X-Cookie-Hash = regsub(req.http.cookie, "KNOWN1=[^;]+;", "");
    set req.http.X-Cookie-Hash = regsub(req.http.X-Cookie-Hash, "KNOWN2=[^;]+;", "");
    hash_data(req.http.X-Cookie-Hash);
}

這會刪除每個已知的cookie並在其余數上散列:)不理想,因為它不能保證標題中cookie的順序,但其他應該起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM