【问题标题】:Varnish serves wrong filesVarnish 提供错误的文件
【发布时间】:2016-05-18 01:05:36
【问题描述】:

我在 nging 后面使用 varnish 3 将多个站点代理到一个域中。 基本设置工作正常,但如果文件名已经存在于它的缓存中,我现在遇到清漆提供错误文件的问题。 基本上我在我的 default.vcl 中所做的就是:

   if(req.url ~ "^/foo1") {
        set req.backend = foo1;
        set req.url = regsub(req.url, "^/foo1/", "/");
    }
    else if(req.url ~ "^/foo2") {
        set req.backend = foo2;
        set req.url = regsub(req.url, "^/foo2/", "/");
    }

如果我现在调用 /foo1/index.html,/foo2/index.html 将提供同一个文件。在重新启动 varnish 并调用 /foo2/index.html 后,/foo1/index.html 将服务于 foo2 的 index.html。

据我所知,这是创建哈希的问题,它不尊重使用的后端,而只尊重 url(缩短后)和域:

    11 VCL_call     c hash
    11 Hash         c /index.html
    11 Hash         c mydomain

我现在通过更改我的 vcl_hash 以也使用后端解决了这个问题,但我确信一定有更好、更方便的方法:

    sub vcl_hash {
      hash_data(req.url);
      hash_data(req.backend);
    }

任何提示将不胜感激,非常感谢!

【问题讨论】:

    标签: varnish varnish-vcl


    【解决方案1】:

    您有两种不同的方法来执行此操作。第一个是通过在vcl_hash 中添加额外值(例如req.backend)来执行您的建议。

    sub vcl_hash {
       hash_data(req.url);
       hash_data(req.backend);
    }
    

    第二种方法,不更新vcl_recv中的req,而只更新vcl_miss/pass中的bereq

    sub vcl_urlrewrite {
        if(req.url ~ "^/foo1") {
            set bereq.url = regsub(req.url, "^/foo1/", "/");
        }
        else if(req.url ~ "^/foo2") {
            set bereq.url = regsub(req.url, "^/foo2/", "/");
        }
    }
    sub vcl_miss {
        call vcl_urlrewrite;
    }
    sub vcl_pass {
        call vcl_urlrewrite;
    }
    sub vcl_pipe {
        call vcl_urlrewrite;
    }
    

    第二种方法需要更多的 VCL,但它也有优势。例如,使用varnishlog 分析日志时,您可以看到原始请求(c 列),以及更新的后端请求(b 列)。

    $ varnishlog /any-options-here/
    (..)
       xx RxURL        c /foo1/index.html
    (..)
       xx TxURL        c /index.html
    (..)
    $ 
    

    【讨论】:

    • 谢谢!第二种方法看起来和工作正常,刚刚经过测试!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2019-08-08
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多