【问题标题】:Varnish not processing ESI includes清漆不处理 ESI 包括
【发布时间】:2012-04-22 22:39:12
【问题描述】:

我正在尝试设置 Varnish 以在本地环境中处理 ESI 包含。

我在虚拟机中运行 varnish,内容在主机上运行。

我有两个文件“index.html”和“test.html”。它们都存储在 apache 服务器的 docroot 中名为“esi”的文件夹中。

index.html

<h1>It Works!</h1>
<esi:include src="test.html" /> 

test.html

<p>ESI HAS BEEN INCLUDED</p>

Varnish 正在虚拟机的 8000 端口上运行。所以我在这里访问它:http://192.168.56.101:8000/esi/

在虚拟机上的 /etc/varnish/default.vcl 中,我在文件底部添加了以下配置:

sub vcl_fetch {
   set beresp.do_esi = true; /* Do ESI processing               */
   set beresp.ttl = 24 h;    /* Sets the TTL on the HTML above  */
}

它应该处理所有请求的 ESI 的想法(不要关心它是否只是试图让这个东西工作的不好的做法:))

我加载http://192.168.56.101:8000/esi/时的结果是:

<h1>It Works!</h1>
<esi:include src="test.html" />

即。 ESI 显示在标记中,它没有被处理。

我检查了 Varnish 日志,但其中没有错误,也没有与 ESI 相关的内容。

谁能看到我在这里做错了什么?如果需要更多信息,请告诉我。谢谢

【问题讨论】:

  • 您使用的是 Varnish > 3.0 吗?因为您使用的是新语法...

标签: varnish varnish-vcl esi


【解决方案1】:

确保您的配置被 varnish 读取。如果您在 docker 容器中运行 varnish,您可能需要重新构建它。

您可以将您的配置更改为一些可笑的东西,看看它是否能捕捉到它。例如将后端更改为 w3.org。

如果这仍然给你同样的结果,你的配置没有被使用。

【讨论】:

    【解决方案2】:

    对于 ESI 作品(varnish 3.x), 第一个字符必须是“

    这是我的测试:

    index.php

    <html>
    <head>
        <title></title>
    </head>
    <body>
    <?php
    
        $now = new \DateTime('now');
        echo "hello world from index.php ".$now->format('Y-m-d H:i:s');
    ?>
    
    <br/>
    
    <esi:include src="/date.php"/>
    
    <br/>
    
    <esi:remove>
        ESI NOT AVAILABLE
    </esi:remove>
    
    <br/>
    
    <!--esi
    ESI AVAILABLE !!
    
    -->
    </body>
    </html>
    

    date.php

    <?php
    $now = new \DateTime('now');
    echo "hello world from date.php ".$now->format('Y-m-d H:i:s');
    

    输出:

    hello world from index.php 2014-08-21 10:45:29
    hello world from date.php 2014-08-21 10:46:35
    

    【讨论】:

      【解决方案3】:

      Varnish 只实现了 ESI 的一小部分。从 2.1 开始,三个 ESI 语句:

          esi:include
          esi:remove
          <!--esi ...-->
      

      基于变量和 cookie 的内容替换尚未实现,但已在路线图上。 Varnish 不会处理 HTML cmets 中的 ESI 指令。 要使 ESI 工作,您需要在 VCL 中激活 ESI 处理,如下所示:

      sub vcl_fetch {
      if (req.url == "/index.html") {
         set beresp.do_esi = true; /* Do ESI processing               */
         set beresp.ttl = 24 h;    /* Sets the TTL on the HTML above  */
      } elseif (req.url == "/test.html") {
         set beresp.ttl = 1m;      /* Sets a one minute TTL on        */
                                   /*  the included object            */
      }
      

      }

      【讨论】:

        【解决方案4】:

        如果您的 esi include src 是“test.html”,那么 varnish 会将该请求发送到您的默认后端,即 127.0.0.1。我相信您需要为远程服务器配置第二个后端。像这样的:

        backend default {
            .host = "127.0.0.1";
            .port = "8000";
        }
        
        backend hostmachine {
            .host = "50.18.104.129"; # Enter your IP address here
            .port = "80";
        }
        

        然后在您的 sub vcl_recv 中,您需要将 URL 中包含 /esi/ 的流量重定向到远程服务器。

        sub vcl_recv {
              if (req.url ~ "^/esi/") {
                    set req.backend = hostmachine;
                    set req.http.host = "www.correctdomainname.com";
              } else {
                    set req.backend = default;
              }
        }
        

        我现在也在做同样的事情,所以试试看,如果它适合你,请告诉我。

        【讨论】:

        • 这个或其他答案如何成为答案? OP(和我)想要包含来自同一服务器的内容,即使 sub vcl_fetch 包含 set beresp.do_esi = true; varnish 也不会费心向后端发出包含请求,而只是在响应中转储 esi 标记。这在谷歌中显示很高,但我看不出这个或其他答案实际上是一个答案。
        猜你喜欢
        • 2011-05-29
        • 2015-10-21
        • 2013-07-16
        • 1970-01-01
        • 2012-11-02
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多