清漆4:如何使用cookie清除缓存

时间:2017-03-02 12:41:32

标签: varnish varnish-vcl varnish-4

当人们将语言从西班牙语改为其他任何语言时,我们的软件使用cookie“selected_language”。默认情况下,cookie未设置。

我们需要考虑语言来缓存我们的网站,因此我们的index.php被缓存多次,每种语言一个。

我们的哈希函数使用语言cookie(我们得到更多的cookie,但它们在vcl_recv中被删除,只是语言cookie(如果有的话)进入哈希例程:

sub vcl_hash {
  hash_data(req.url);
  if (req.http.host) {
    hash_data(req.http.host);
  } else {
    hash_data(server.ip);
  }
  if (req.http.Cookie) {
    hash_data(req.http.Cookie);
  }
}

当我们要清除缓存时,会出现问题。 如果我们清除我们的索引网页,没有cookie(西班牙语版本),一切正常:

curl -X PURGE  http://www.arasaac.org/index.php

但是,如果我们尝试使用cookies(英文版)清除我们的索引网页,它什么都不做。 我认为purge使用hash,所以如果我发送相关的cookie它应该可以工作。

我们的清除代码:

sub vcl_purge {
  # Only handle actual PURGE HTTP methods, everything else is discarded
  if (req.method != "PURGE") {
    # restart request
    set req.http.X-Purge = "Yes";
    return(restart);
  }
}

内部vcl_recv:

  # Allow purging
  if (req.method == "PURGE") {
#    if (!client.ip ~ purge) { # purge is the ACL defined at the begining
      # Not from an allowed IP? Then die with an error.
#      return (synth(405, "This IP is not allowed to send PURGE requests."));
#    }
    # If you got this stage (and didn't error out above), purge the cached result
    return (purge);
  }

这些是所有日志,以防他们需要......

我再次询问缓存的页面:

*   << Request  >> 262149    
-   Begin          req 262148 rxreq
-   Timestamp      Start: 1488455938.456980 0.000000 0.000000
-   Timestamp      Req: 1488455938.456980 0.000000 0.000000
-   ReqStart       172.20.0.3 51672
-   ReqMethod      GET
-   ReqURL         /index.php
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: www.arasaac.org
-   ReqHeader      Connection: close
-   ReqHeader      X-Real-IP: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-Proto: http
-   ReqHeader      X-Forwarded-Ssl: off
-   ReqHeader      X-Forwarded-Port: 80
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      Referer: http://www.arasaac.org/index.php
-   ReqHeader      Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Language: es-ES,es;q=0.8,en;q=0.6
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqUnset       X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1, 172.20.0.3
-   VCL_call       RECV
-   ReqUnset       Host: www.arasaac.org
-   ReqHeader      Host: www.arasaac.org
-   ReqURL         /
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _ga=GA1.2.99795965.1488449144; _gat=1
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   Hit            3
-   VCL_call       HIT
-   VCL_return     deliver
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Date: Thu, 02 Mar 2017 11:57:52 GMT
-   RespHeader     Server: Apache/2.4.10 (Debian)
-   RespHeader     X-Powered-By: PHP/5.6.30
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Content-Length: 5379
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     cache-control: public, max-age = 300
-   RespHeader     log: ha entrado aquí
-   RespHeader     X-CacheReason: varnishcache
-   RespHeader     X-Varnish: 262149 3
-   RespHeader     Age: 66
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     X-Cache: HIT
-   RespHeader     X-Cache-Hits: 1
-   RespUnset      X-Powered-By: PHP/5.6.30
-   RespUnset      Server: Apache/2.4.10 (Debian)
-   RespUnset      X-Varnish: 262149 3
-   RespUnset      Via: 1.1 varnish-v4
-   VCL_return     deliver
-   Timestamp      Process: 1488455938.457118 0.000138 0.000138
-   RespHeader     Accept-Ranges: bytes
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: close
-   Timestamp      Resp: 1488455938.457194 0.000215 0.000077
-   ReqAcct        658 0 658 336 5379 5715
-   End    

我尝试清除页面,因此我发送了cookie:

curl -X PURGE --cookie“selected_language = en”http://www.arasaac.org/index.php

日志对我来说似乎很好:

*   << Request  >> 28        
-   Begin          req 27 rxreq
-   Timestamp      Start: 1488456128.433721 0.000000 0.000000
-   Timestamp      Req: 1488456128.433721 0.000000 0.000000
-   ReqStart       172.20.0.3 53286
-   ReqMethod      PURGE
-   ReqURL         /index.php
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: www.arasaac.org
-   ReqHeader      Connection: close
-   ReqHeader      X-Real-IP: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-Proto: http
-   ReqHeader      X-Forwarded-Ssl: off
-   ReqHeader      X-Forwarded-Port: 80
-   ReqHeader      User-Agent: curl/7.35.0
-   ReqHeader      Accept: */*
-   ReqHeader      Cookie: selected_language=en;
-   ReqUnset       X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1, 172.20.0.3
-   VCL_call       RECV
-   ReqUnset       Host: www.arasaac.org
-   ReqHeader      Host: www.arasaac.org
-   ReqURL         /
-   VCL_return     purge
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       PURGE
-   VCL_return     synth
-   Timestamp      Process: 1488456128.433754 0.000033 0.000033
-   RespHeader     Date: Thu, 02 Mar 2017 12:02:08 GMT
-   RespHeader     Server: Varnish
-   RespHeader     X-Varnish: 28
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespReason     Purged
-   VCL_call       SYNTH
-   VCL_return     deliver
-   RespHeader     Content-Length: 0
-   Storage        malloc Transient
-   RespHeader     Accept-Ranges: bytes
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: close
-   Timestamp      Resp: 1488456128.433792 0.000070 0.000038
-   ReqAcct        261 0 261 152 0 152
-   End 

但是,如果我在浏览器中再次加载index.php,缓存仍然存在:

<< Request  >> 30        
-   Begin          req 29 rxreq
-   Timestamp      Start: 1488456171.661988 0.000000 0.000000
-   Timestamp      Req: 1488456171.661988 0.000000 0.000000
-   ReqStart       172.20.0.3 53688
-   ReqMethod      GET
-   ReqURL         /index.php
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: www.arasaac.org
-   ReqHeader      Connection: close
-   ReqHeader      X-Real-IP: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-Proto: http
-   ReqHeader      X-Forwarded-Ssl: off
-   ReqHeader      X-Forwarded-Port: 80
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
-   ReqHeader      Referer: http://www.arasaac.org/index.php
-   ReqHeader      Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Language: es-ES,es;q=0.8,en;q=0.6
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqUnset       X-Forwarded-For: 172.20.0.1
-   ReqHeader      X-Forwarded-For: 172.20.0.1, 172.20.0.3
-   VCL_call       RECV
-   ReqUnset       Host: www.arasaac.org
-   ReqHeader      Host: www.arasaac.org
-   ReqURL         /
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   ReqUnset       Cookie: selected_language=en; 
-   ReqHeader      Cookie: selected_language=en; 
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, sdch
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   Hit            3
-   VCL_call       HIT
-   VCL_return     deliver
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Date: Thu, 02 Mar 2017 11:57:52 GMT
-   RespHeader     Server: Apache/2.4.10 (Debian)
-   RespHeader     X-Powered-By: PHP/5.6.30
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Content-Length: 5379
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     cache-control: public, max-age = 300
-   RespHeader     log: ha entrado aquí
-   RespHeader     X-CacheReason: varnishcache
-   RespHeader     X-Varnish: 30 3
-   RespHeader     Age: 299
-   RespHeader     Via: 1.1 varnish-v4
-   VCL_call       DELIVER
-   RespHeader     X-Cache: HIT
-   RespHeader     X-Cache-Hits: 2
-   RespUnset      X-Powered-By: PHP/5.6.30
-   RespUnset      Server: Apache/2.4.10 (Debian)
-   RespUnset      X-Varnish: 30 3
-   RespUnset      Via: 1.1 varnish-v4
-   VCL_return     deliver
-   Timestamp      Process: 1488456171.662050 0.000061 0.000061
-   RespHeader     Accept-Ranges: bytes
-   Debug          "RES_MODE 2"
-   RespHeader     Connection: close
-   Timestamp      Resp: 1488456171.662105 0.000117 0.000056
-   ReqAcct        658 0 658 337 5379 5716
-   End          

顺便说一下..与cookie相关的日志中的ReqHeader和Reqset行重复多次?

    -   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
    -   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
    -   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
    -   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
    -   ReqUnset       Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; _ga=GA1.2.99795965.1488449144
    -   ReqHeader      Cookie: PHPSESSID=242b73bc8a560b89308009e05af7eefd; selected_language=en; _gat=1; 
...

1 个答案:

答案 0 :(得分:2)

我的猜测是,当你清理饼干时,最终得到:

"selected_language=en; "(带尾随空格)

而在您的清除请求中,您传递

"selected_language=en;"

尝试像这样发送清除:

curl -X PURGE --cookie "selected_language=en " http://www.arasaac.org/index.php

您在varnishlog中看到的重复行是每次VCL代码尝试取消设置或操纵cookie值时(在清理阶段)。