symfony/symfony — Issue #2640: Esi tags resolving in symfony when using Varnish
Author: innocead
Created: 2011-11-14 13:57:24+00:00
State: open

Configuration: nginx -> varnish -> nginx -> php-fpm.

1. I clean all cache and restart varnish.
2. Open site in browser
3. Look in generated html in http_cache directory.

Content have this code:

esi->handle($this, '/_intrl842/DvjMainBundle%3ADefault%3AtopPanel/none.html', '', false) ?>

Esi tags not present!

Varnish 3.0.2 configuration

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

sub vcl_recv {

set req.http.Surrogate-Capability = "abc=ESI/1.0";

if (req.request == "POST") {
return (pass);
}

return (lookup);
}

sub vcl_fetch {
if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
remove beresp.http.Surrogate-Control;
set beresp.do_esi = true;
}

if (beresp.http.Cache-Control ~ "private") {
set beresp.ttl = 0s;
}

return (deliver);
}

sub vcl_deliver {
if(obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}

i override HttpCache ->processResponseBody method and all works fine. In nginx + symfony and in nginx + varnish + nginx + symfony configurations.

class AppCache extends HttpCache {

protected $esi;

public function __construct(HttpKernelInterface $kernel)
{
$store = new Store($kernel->getCacheDir() . '/http_cache');
$esi = new Esi();
$this->esi = $esi;

parent::__construct($kernel, $store, $esi, array_merge(array('debug' => $kernel->isDebug()), $this->getOptions()));
}

protected function getOptions()
{
return array(
'debug' => true,
);
}

protected function processResponseBody(Request $request, Response $response)
{
if (null !== $this->esi) {
if (null === $value = $request->headers->get('Surrogate-Capability')) {
return;
}

if (0 === strcmp('symfony2="ESI/1.0"', $value)) {
$this->esi->process($request, $response);
}
}
}

}