【问题标题】:Varnish handling Cookie incorrectly. Filtering not working清漆处理 Cookie 不正确。过滤不起作用
【发布时间】:2020-10-02 00:07:45
【问题描述】:

我正在尝试使用 Varnish 来缓存我的网站。但我希望 Varnish 绕过一些带有命名 Cookies(RSS) 的请求。

清漆版本:6.3

这是我的default.vcl的一部分

sub vcl_recv {
    set req.http.X-Varnish-PHP_SID = req.http.Cookie;
    set req.http.X-Varnish-PHP_SID =
       regsuball(req.http.X-Varnish-PHP_SID, ";? ?RSS=([a-zA-Z0-9]+)( |;| ;).*","\1");
    set req.http.Cookie = req.http.X-Varnish-PHP_SID;
    unset req.http.X-Varnish-PHP_SID;
}

这是来自 Varnish 的日志

*   << Request  >> 452
-   Begin          req 411 rxreq
-   Timestamp      Start: 1591947637.399591 0.000000 0.000000
-   Timestamp      Req: 1591947637.400066 0.000475 0.000475
-   VCL_use        boot
-   ReqStart       42.88.136.35 10072 a0
-   ReqMethod      POST
-   ReqURL         /~YeV.js
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: www.ttester.com:8000
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-   ReqHeader      Xiaytvhcnb: liacddpfckbjzpovdxrvjhibipwqevuxivzelicudjgcngcgdvgddndfghzrtvoelxkblzkiwqewvjleubxycuitwojhdztogomqoqtyufkqqqecrfoytklnxztnjytcrfptiplkuidfrbtxgighjpoppdkrjmngknujsgeunfgdcghjejprujdcyqmgkmxwgfdamxofmzjneajjymvqrixfaypudahokxrydpoeuynewshpzq
-   ReqHeader      Accept-Encoding: gzip, deflate
-   ReqHeader      Accept-Language: en-us
-   ReqHeader      Content-Type: application/x-www-form-urlencoded
-   ReqHeader      Origin: http://www.ttester.com:8000
-   ReqHeader      User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      Referer: http://www.ttester.com:8000/~YeV.js
-   ReqHeader      Content-Length: 8
-   ReqHeader      Cookie:
-   ReqHeader      Cookie: BAIDU-STFS=6352be5e1748a73bfe4c898d9805215c56d6b8a9e6d30b2549168ae853c76078; RSS=1; Hm_lpvt_12e92bc53a305b423a5f20bd34b0a75e=1591947600; Hm_lvt_12e92bc53a305b423a5f20bd34b0a75e=1591936468; Hm_lpvt_12e92bc53a305b423a5f20bd34b0a75e=1591944261; Hm_l
-   ReqHeader      X-Forwarded-For: 42.88.136.35
-   VCL_call       RECV
-   ReqHeader      X-Varnish-PHP_SID:
-   ReqUnset       X-Varnish-PHP_SID:
-   ReqHeader      X-Varnish-PHP_SID:
-   ReqUnset       Cookie:
-   ReqUnset       Cookie: BAIDU-STFS=6352be5e1748a73bfe4c898d9805215c56d6b8a9e6d30b2549168ae853c76078; RSS=1; Hm_lpvt_12e92bc53a305b423a5f20bd34b0a75e=1591947600; Hm_lvt_12e92bc53a305b423a5f20bd34b0a75e=1591936468; Hm_lpvt_12e92bc53a305b423a5f20bd34b0a75e=1591944261; Hm_l
-   ReqHeader      Cookie:
-   ReqUnset       X-Varnish-PHP_SID:
-   ReqUnset       Host: www.ttester.com:8000
-   ReqHeader      host: www.ttester.com:8000
-   VCL_return     pass
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       PASS
-   VCL_return     fetch
-   Link           bereq 453 pass
-   Storage        malloc Transient
-   Timestamp      ReqBody: 1591947637.400221 0.000629 0.000154
-   Timestamp      Fetch: 1591947637.407862 0.008270 0.007641
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Date: Fri, 12 Jun 2020 07:40:37 GMT
-   RespHeader     Server: Apache/2.4.25 (Debian)
-   RespHeader     X-Powered-By: PHP/7.2.6
-   RespHeader     URL-Record-File: www.ttester.com/~YeV.js
-   RespHeader     URL-Cluster: cn5
-   RespHeader     cache-control: no-store, no-cache, must-revalidate
-   RespHeader     Set-Cookie: BAIDU-STFS=6352be5e1748a73bfe4c898d9805215c56d6b8a9e6d30b2549168ae853c76078; Expires=Friday, 12-Jun-2020 15:40:39 CST; Max-Age=2; path=/
-   RespHeader     Set-Cookie: RSS=1; Expires=Friday, 12-Jun-2020 15:40:39 CST; Max-Age=2; path=/
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Content-Length: 626
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     V-Cache-TTL: 0.000
-   RespHeader     V-Cache-Grace: 10.000
-   RespHeader     X-Varnish: 452
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.3)
-   VCL_call       DELIVER
-   RespHeader     V-Cache: MISS
-   RespUnset      Server: Apache/2.4.25 (Debian)
-   RespHeader     Server: MFPad
-   RespUnset      X-Powered-By: PHP/7.2.6
-   RespUnset      Via: 1.1 varnish (Varnish/6.3)
-   RespUnset      X-Varnish: 452
-   VCL_return     deliver
-   Timestamp      Process: 1591947637.407883 0.008292 0.000021
-   Filters
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1591947637.407919 0.008328 0.000036
-   ReqAcct        2056 8 2064 633 626 1259
-   End

我还尝试了另一个版本的 VCL,如下所示,但效果不佳。

sub vcl_recv {
  if (req.http.Cookie && !(req.http.Cookie ~ "RSS=")) {
    unset req.http.Cookie;
  }
}

我只是希望 Varnish 可以绕过带有 RSS Cookie 的请求。

【问题讨论】:

    标签: varnish


    【解决方案1】:

    如果我理解正确,您希望缓存所有页面,但包含 RSS cookie 的页面除外。

    这是老式的丑陋做法:

    sub vcl_recv {
      if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie, ";(RSS)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
    
        if (req.http.cookie ~ "^\s*$") {
          unset req.http.cookie;
        }
      }
    }
    

    但是,如果您要升级到 Varnish Cache 6.4,您将在该版本中打包 vmod_cookie,这使得 cookie 操作更加容易。

    这是使用vmod_cookie 的工作原理:

    import cookie;
    
    sub vcl_recv {
        cookie.parse(req.http.cookie);
        cookie.keep("RSS");
        set req.http.cookie = cookie.get_string();
    }
    

    两种实现都依赖于默认的 Varnish 行为。这意味着如果在替换 cookie 之后,还有一个 cookie(在我们的例子中为 RSS,Varnish 将不会缓存并在 vcl_recv 挂钩中执行 return(pass)

    这是你要找的吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 2012-08-19
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2018-01-22
      • 2015-10-18
      • 2013-06-26
      相关资源
      最近更新 更多