Varnish :有条件地清除缓存以确保资源是最新的

标签 varnish varnish-vcl

假设我有一个“资源” - http://localhost/doc ,我希望它一直被 Varnish 缓存 因为它经常被访问, 除了 有人通过 POST 更新它,POST 更新完成后,所有进一步的请求都应该检索最新版本的“文档”。

我的想法是将此逻辑包装在我的后端服务器中,因为默认情况下 POST 请求将通过 Varnish 而不进行缓存,而在我的后端服务器中,我可以:

  • 执行命令:varnishadm -T 127.0.0.1:6082 purge req.url == "/doc"
  • 发出 HTTP 清除请求:curl -X PURGE http://localhost/doc

  • 但是,它需要我配置 VCL 以支持“Cache-Control” header !
    我的 VCL 如下所示,但不起作用:
    sub vcl_fetch {
        if (beresp.http.Cache-Control ~ "no-cache") {
            purge;
        }
        return (deliver);
    }
    acl purgers { "127.0.0.1"; }
    
    sub vcl_recv {
        if (req.request == "PURGE") {
            if (!client.ip ~ purgers) {
                error 405 "Method not allowed";
            }
            return (lookup);
        }
    }
    sub vcl_hit {
        if (req.request == "PURGE") {
            #purge;
            purge("req.url ~ " req.url); 
            error 200 "Purged";
        }
    }
    sub vcl_miss {
        if (req.request == "PURGE") { 
            purge;
            error 200 "Purged";
        }
    }
    sub vcl_pass {
        if (req.request == "PURGE") {
            error 502 "PURGE on a passed object";
        }
    }
    

    它提示:
    Message from VCC-compiler:
    Expected ';' got '('
    
    (program line 174), at
    ('input' Line 154 Pos 14)
            purge("req.url ~ " req.url); 
    -------------#-----------------------
    
    Running VCC-compiler failed, exit 1
    
    VCL compilation failed
    
  • 问题 1:如何更新 VCL 以实现我的目标?
  • 问题 2:我
    认为这是一个很常见的场景:缓存资源同时保留它
    最新的,那么使用实现这一目标的最佳实践是什么
    漆?

  • 感谢您的任何回应!

    最佳答案

    当文档更新时,您的应用程序服务器可以发出清除URL的请求。它需要为文档发送PURGE请求,而不是GET请求。

    另外,当您收到该网址的POST时,可以对该网址发布禁令。

    关于 Varnish :有条件地清除缓存以确保资源是最新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14229301/

    相关文章:

    varnish - Varnish hit-for-pass 是什么意思?

    varnish - 试图破译这个 Varnish vcl block (与恩典有关)

    php - 取消设置 php 中的变量会影响缓存吗?

    varnish - 在Varnish 4中 'synthetic'设置了哪个变量?

    caching - 为什么 Varnish 缓存 ESI 片段(当我告诉它不缓存时)?

    ruby-on-rails - 安装 SSL 后在生产模式下显示 Rails 开发风格的错误

    varnish - 在哪里将purge.php文件放在 Varnish 服务器中?

    amazon-web-services - ELB 保活超时 : Varnish Configuration Assistance

    caching - Varnish :禁止使用任何GET参数的URL

    symfony - Varnish panic -废弃的原木(vsl)