基本
我做了一个非常简单的测试动作:
/**
* @Route("/public/debug/varnish", name="debug_varnish")
* @Template
*/
public function varnishAction()
{
return [];
}
{# varnish.html.twig #}
<html>
<body>
<h1>Layout</h1>
<hr />
{{ render_esi(controller('TestBundle:Debug:esi')) }}
</body>
</html>
包含的动作和树枝现在并不重要。
考试
Varnish 需要Surrogate-Control: content="ESI/1.0"
响应中的标头。看看会发生什么:
curl -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
响应:
<html>
<body>
<h1>Layout</h1>
<hr />
<esi:include src="/_proxy?_path=_format%3Dhtml%26_locale%3Den_US%26_controller%3DTestBundle%253ADebug%253Aesi" onerror="continue" />
</body>
</html>
一切安好!但看看标题:
curl -I -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
输出:
HTTP/1.1 200 OK
Server: nginx/1.2.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1~dotdeb.1
Set-Cookie: PHPSESSID=k8jii3gkrha1js7edbbbqjjh32; path=/
Cache-Control: no-cache
Date: Tue, 18 Feb 2014 11:21:04 GMT
没有Surrogate-Control
标题!
\Symfony\Component\HttpKernel\HttpCache\Esi 类
有一个非常非常简单的函数,如果内容包含字符串,则添加Surrogate-Control
标题:<esi:include
/**
* Adds HTTP headers to specify that the Response needs to be parsed for ESI.
*
* This method only adds an ESI HTTP header if the Response has some ESI tags.
*
* @param Response $response A Response instance
*/
public function addSurrogateControl(Response $response)
{
if (false !== strpos($response->getContent(), '<esi:include')) {
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
}
}
但事实并非如此!如果是“真”。当我用这个打电话时:
public function addSurrogateControl(Response $response)
{
var_dump(false !== strpos($response->getContent(), '<esi:include'));
if (false !== strpos($response->getContent(), '<esi:include')) {
echo "before add header\n";
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
echo "after add header\n";
}
echo "after if"
}
curl -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
响应:
(bool) true
before add header
after add header
after if
<html>
<body>
<h1>Layout</h1>
<hr />
<esi:include src="/_proxy?_path=_format%3Dhtml%26_locale%3Den_US%26_controller%3DTestBundle%253ADebug%253Aesi" onerror="continue" />
</body>
</html>
curl -I -H 'host:www.domain.com' -H 'Surrogate-Capability: abc=ESI/1.0' http://127.0.0.1:8080/public/debug/varnish
输出:
HTTP/1.1 200 OK
Server: nginx/1.2.1
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1~dotdeb.1
Set-Cookie: PHPSESSID=k8jii3gkrha1js7edbbbqjjh32; path=/
Cache-Control: no-cache
Date: Tue, 18 Feb 2014 11:21:04 GMT
标题在哪里Surrogate-Control
?:-o
更多测试:
public function addSurrogateControl(Response $response)
{
// [1]
if (false !== strpos($response->getContent(), '<esi:include')) {
// [2]
}
// [3]
}
和$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
(!!!) header('Test: test OK')
:
- [1] 地点:工作!
- [2]原来的地方:不起作用:(
- [3] 地点:工作!
好的。因此,如果任何标头修改位于 中IF
,则它不起作用。为什么?
魔法
当我改变条件$response->getContent()
时'test <esi:include test'
,if
一切正常。类Response
函数getContent()
:
public function getContent()
{
return $this->content;
}
我不明白 :-/