【问题标题】:Varnish request missing cache despite being in清漆请求丢失缓存,尽管在
【发布时间】:2016-03-24 03:00:17
【问题描述】:

我在 Varnish 缓存中有这个请求:

ReqMethod      GET
ReqURL         /organisation/xyz/proposal_0000000/comments/comment_0000001/

然后将一些 PURGE 请求发送到 Varnish,从而生成以下禁令列表:

ban.list 200 2108
Present bans: 1458150360.937187 16 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/team0000000$ 1458150360.929092 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz$ 1458150360.926030 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/pitch0000000$ 1458150360.923491 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/practicalrelevance0000000$ 1458150360.921025 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/plan0000000$ 1458150360.918480 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/target0000000$ 1458150360.915931 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/duration0000000$ 1458150360.913486 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/difference0000000$ 1458150360.910710 0 - req.http.host == localhost:8088 && req.url ~ /$ 1458150360.908150 0 - req.http.host == localhost:8088 && req.url ~ /organisation$ 1458150360.906249 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/extrainfo0000000$ 1458150360.904289 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/connectioncohesion0000000$ 1458150360.901930 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/challenge0000000$ 1458150360.899287 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/goal0000000$ 1458150360.896989 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/partners0000000$ 1458150360.894324 0 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000$ 1458150360.891701 0 C
1458150348.035639 1 C

然后再次执行相同的 GET 请求,但有一个 MISS:

* << Request >> 32790
- Begin req 32789 rxreq - Timestamp Start: 1458150371.759282 0.000000 0.000000 - Timestamp Req: 1458150371.759282 0.000000 0.000000 - ReqStart 127.0.0.1 43526 - ReqMethod GET - ReqURL /organisation/xyz/proposal_0000000/comments/comment_0000001/ - ReqProtocol HTTP/1.1 - ReqHeader Host: localhost:8088 - ReqHeader Connection: keep-alive - ReqHeader Pragma: no-cache - ReqHeader Cache-Control: no-cache - ReqHeader Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 - ReqHeader Upgrade-Insecure-Requests: 1 - ReqHeader Referer: http://localhost:8088/organisation/xyz/proposal_0000000/comments/?elements=paths - ReqHeader Accept-Encoding: gzip, deflate, sdch - ReqHeader Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 - ReqHeader X-Forwarded-For: 127.0.0.1 - VCL_call RECV - VCL_return hash - ReqUnset Accept-Encoding: gzip, deflate, sdch - ReqHeader Accept-Encoding: gzip - VCL_call HASH - VCL_return lookup - ExpBan 3 banned lookup - VCL_call MISS - VCL_return fetch - Link bereq 32791 fetch - Timestamp Fetch: 1458150371.779571 0.020289 0.020289 - RespProtocol HTTP/1.1 - RespStatus 200 - RespReason OK - RespHeader Server: gunicorn/19.2.1 - RespHeader Date: Wed, 16 Mar 2016 17:46:11 GMT - RespHeader X-Caching-Mode: with_proxy_cache - RespHeader X-Caching-Strategy: HTTPCacheStrategyWeakAdapter - RespHeader Cache-Control: max-age=0, proxy-revalidate, s-maxage=31104000 - RespHeader Vary: Accept-Encoding, X-User-Path, X-User-Token - RespHeader Content-Type: application/json; charset=UTF-8 - RespHeader Access-Control-Allow-Origin: * - RespHeader Access-Control-Allow-Methods: POST,GET,DELETE,PUT,OPTIONS - RespHeader Access-Control-Allow-Headers: Origin, Content-Type, Accept, X-User-Path, X-User-Token - RespHeader ETag: W/"0|1|2016-03-16 13:44:05.887212+00:00|None|None" - RespHeader Content-Encoding: gzip - RespHeader X-Varnish: 32790 - RespHeader Age: 0 - RespHeader Via: 1.1 varnish-v4 - VCL_call DELIVER - VCL_return deliver - Timestamp Process: 1458150371.779598 0.020317 0.000028 - RespHeader Accept-Ranges: bytes - RespHeader Content-Length: 426 - Debug "RES_MODE 2" - RespHeader Connection: keep-alive - Timestamp Resp: 1458150371.779641 0.020359 0.000042 - ReqAcct 598 0 598 699 426 1125 - End

然后是禁止列表:

ban.list 200 147
Present bans: 1458150360.937187 17 - req.http.host == localhost:8088 && req.url ~ /organisation/xyz/proposal_0000000/team0000000$

我知道正则表达式。 /organisation/xyz/proposal_0000000/comments/comment_0000001/ 如何匹配ban.list 中的任何模式?这没有意义。

我正在使用 Varnish 4.1.1

【问题讨论】:

    标签: varnish


    【解决方案1】:

    与您的网址匹配的规则是:

    1458150360.910710     0 -  req.http.host == localhost:8088 && req.url ~ /$
    

    regex req.url ~ /$ 将匹配您的 URL 以及任何其他以斜杠结尾的 URL;使用 req.url ~ ^/$

    一些观察:

    关于对潜伏者友好的禁令表达

    阅读更多here

    对潜伏者友好的禁令表达式是那些只使用 obj. 而不是 req. 变量的表达式。由于对 lurker 友好的禁令表达式缺少 req.,您可能需要将一些 req. 内容复制到 obj 结构中。实际上,这种复制操作是一种将客户端请求的上下文保存在缓存对象中的机制。例如,您可能希望将客户端上下文的有用部分,例如请求的 URL 从 req 复制到 obj。

    下面的 sn-p 显示了如何在缓存对象中保留客户端请求的上下文的示例:

    sub vcl_backend_response {
       set beresp.http.x-url = bereq.url;
    }
    
    sub vcl_deliver {
       # The X-Url header is for internal use only
       unset resp.http.x-url;
    }
    

    正则表达式的清漆测试示例:

    您可以使用以下命令运行它:varnishtest test_regex.vtc

    test_regex.vtc 内容:

    # act like a backend server
    server s1 {
        rxreq
        txresp
        expect req.url == "/organisation/xyz/proposal_0000000/comments/comment_0000001/"
        expect req.http.Test == "dosent_match" 
    } -start
    
    # define & start a varnish instance
    varnish v1 -vcl {
        backend default {
        .host = "${s1_addr}";
        .port = "${s1_port}";
        }
    
        sub vcl_recv {
        if ( req.url ~ "/$" ) {
            set req.http.Test="match";
        } else {
            set req.http.Test="dosent_match";
        }
      }
    
    } -start
    
    # make a client request
    client c1 { 
    
        txreq -url "/organisation/xyz/proposal_0000000/comments/comment_0000001/"
        rxresp
    
    } -run
    
    varnish v1 -expect client_req == 1
    

    【讨论】:

    • 但为什么/organisation/xyz/proposal_0000000/comments/comment_0000001/ 匹配其中一项禁令?
    • 因为您不强制修复开头,所以使用正则表达式“/$”的禁令将匹配任何以斜杠结尾的 URL(您也应该使用:“^/$”来限制开头)。请参阅此处的一些示例gist.github.com/dimsemenov/10100415
    • 我明白了。我错过了这个,即使在阅读了你的评论之后。估计我累了。感谢您的帮助!
    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2017-10-20
    • 2012-08-07
    • 2015-10-10
    • 1970-01-01
    • 2020-07-26
    相关资源
    最近更新 更多