简体   繁体   English

清漆不处理 ESI 包括

[英]Varnish not processing ESI includes

I'm trying to setup Varnish to process ESI includes on a local environment.我正在尝试设置 Varnish 以在本地环境中处理 ESI 包含。

I am running varnish in a virtual machine and the content is running on the host machine.我在虚拟机中运行清漆,内容在主机上运行。

I have two files "index.html" and "test.html".我有两个文件“index.html”和“test.html”。 These are both stored in a folder called "esi" in the docroot of an apache server.它们都存储在 apache 服务器的文档根目录中名为“esi”的文件夹中。

index.html index.html

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

test.html测试.html

<p>ESI HAS BEEN INCLUDED</p>

Varnish is running on the virtual machine on port 8000. So I access it here: http://192.168.56.101:8000/esi/ Varnish 在虚拟机的 8000 端口上运行。所以我在这里访问它: http://192.168.56.101:8000/esi/

in /etc/varnish/default.vcl on the virtual machine I have added the followin config to the bottom of the file:在虚拟机的 /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  */
}

With the idea that it should process ESI on ALL requests (Dont care if its bad practice just trying to get this thing to work:))考虑到它应该处理所有请求的 ESI(不要关心它是否只是试图让这个东西工作的坏做法:))

The result when I load http://192.168.56.101:8000/esi/ is:当我加载http://192.168.56.101:8000/esi/时的结果是:

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

ie. IE。 the ESI is shown in the markup, it is not being processed. ESI 显示在标记中,它未被处理。

I have checked the Varnish log, however there are no errors in there and nothing related to ESIs.我已经检查了 Varnish 日志,但是那里没有错误,也没有与 ESI 相关的内容。

Can anyone see what I am doing wrong here?谁能看到我在这里做错了什么? Let me know if more information is needed.. thanks让我知道是否需要更多信息..谢谢

For ESI works (varnish 3.x), the first char must be a "<" so simply add HTML structure对于 ESI 作品(varnish 3.x),第一个字符必须是“<”所以只需添加 HTML 结构

Here my test:这是我的测试:

index.php 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

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

Output: Output:

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

If your esi include src is "test.html" then varnish will be sending that request to your default backend, which is 127.0.0.1.如果您的 esi include src 是“test.html”,那么 varnish 会将该请求发送到您的默认后端,即 127.0.0.1。 I believe you need to configure a second backend for your remote server.我相信您需要为您的远程服务器配置第二个后端。 Something like this:像这样:

backend default {
    .host = "127.0.0.1";
    .port = "8000";
}

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

Then in your sub vcl_recv you need to redirect traffic that has /esi/ in the URL to the remote server.然后在您的子 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;
      }
}

I'm working on the same thing right now so give it a try and let me know if it works for you.我现在正在做同样的事情所以试一试,让我知道它是否适合你。

Varnish only implemented a small subset of ESI. Varnish 只实现了 ESI 的一小部分。 As of 2.1 three ESI statements:截至 2.1 三个 ESI 声明:

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

Content substitution based on variables and cookies is not implemented but is on the roadmap.基于变量和 cookies 的内容替换未实现,但在路线图上。 Varnish will not process ESI instructions in HTML comments. Varnish 不会处理 HTML 注释中的 ESI 指令。 For ESI to work you need to activate ESI processing in VCL, like this:要使 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            */
}

} }

Make sure your config is read by varnish.确保你的配置被清漆读取。 If you are running varnish in a docker container you might have to rebuild it.如果您在 docker 容器中运行清漆,您可能需要重建它。

You can change your config to something ridiculous and see if it catches it.您可以将您的配置更改为一些荒谬的东西,看看它是否能捕捉到它。 For instance change the backend to w3.org.例如,将后端更改为 w3.org。

If this still gives you the same result, you config isn't used.如果这仍然给你同样的结果,你的配置没有被使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM