Nginx – Varnish ESI not working

apache-2.2nginxvarnish

I have a configuration like this:
– nginx port 80
– varnish (3.0.4) port 6081
– apache port 8080

Nginx takes the request and passes it to Varnish which then check the cache and then either returns the response from cache or passes the request to Apache.
In Apache I've disabled mod_deflate so the output is not gzipped.
Inside Varnish I've enabled ESI for all requests like this:

sub vcl_fetch {
    set beresp.do_esi = true;
}

And my test file (test.php) looks like this:

Current time is: <esi:include src="/date.php" /> 

The date.php:

<?php
echo date('H:i:s');

But Varnish is nos processing the esi include. In varnishlog I get this error:

11 ESI_xmlerror c No ESI processing, first char not '<'

Response headers from test.php:

Accept-Ranges:bytes
Age:3
Connection:keep-alive
Content-Length:51
Content-Type:text/html
Date:Sun, 01 Sep 2013 11:51:57 GMT
Server:nginx
Surrogate-Control:"ESI/1.0"
Via:1.1 varnish
X-Powered-By:PHP/5.4.15-1~precise+1
X-Varnish:1236304062 1236304061

And the html output:

Current time is: <esi:include src="/name.php" /> 

So you can see ESI is not processed.

What am I doing wrong?

Best Answer

Solved it... if the first character of the server response isn't "<" ESI will not work.. The solution for my problem was just to wrap the test file in standard HTML structure, so that it looks like this:

<html>
<head>
    <title></title>
</head>
<body>
Current time is: <esi:include src="/name.php" /> 
</body>
</html>