How configure Varnish 3 for use with ESI

One of improvements that I was impressed on the last version of varnish is the support of ESI language and, although the previous version was implemented part of this language, this did not works well.

Cache diagram with ESIFor the people that don’t know that I’m talking, ESI is a mark up language that permit mark parts of a web page for are cached for independent time of the rest of the web. Imagine that you have in your home a part that is 100% static (logo, menu,..) and you have other dynamically parts (a news column for example). The traditional systems, until now, only permit cached a full page so if you want update the news column, you have send to you cache system the order for update the full page (include the static parts that you do not updated). With ESI, you can indicate that only update a part of the page (in this case, the news column) so rest page is not updated saving the memory, cpu and traffic cost.

I am still doing tests with this technology but if you can test too and don’t know how start, next I write a simple example to start the testing:

For example, imagine that you have 3 files. The first will call hour1.php that contain the next code:

Download hour1.php
1
2
<?php $hourUnix = time(); ?>
<p><?php echo date('Y-m-d H:i:s',$hourUnix); ?></p>

If you execute this code, you can view the date and hour of the system. Next create other file that will call hour2.php that contain the same code. You will see why after.

Download hour2.php
1
2
<?php $hourUnix = time(); ?>
<p><?php echo date('Y-m-d H:i:s',$hourUnix); ?></p>

Next, the file index.html that contain the next code:

Download index.html
1
2
3
4
5
6
<html>
  <body>
    <esi:include src="hour1.php" />
    <esi:include src="hour2.php" />
  </body>
</html>

Now, probably you can see the idea of this experiment. In index.html file is indicated that it must include 2 files that show something (in this case, show the system date and hour).

And next, here comes the most interesting, the Varnish configuration. For the before code works, you must enable the ESI language on Varnish configuration and indicate the cache time for all parts of independent code. For make this, you must add in Varnish config file (/etc/varnish/default.vcl by default) the next code:

View Code SHELL
1
2
3
4
5
6
7
8
9
10
11
12
sub vcl_fetch {
 ...
      if (req.url == "/") { /* if is the home */
              set beresp.do_esi = true; /* Active ESI language */
              set beresp.ttl = 24 h;    /* The cache time of the home is 24 hours  */
      } elseif (req.url == "/hour1.php") { /* if is the 1 part of the home */
              set beresp.ttl = 1m;      /* The cache time for this fragment is 1 minute */
      } elseif (req.url == "/hour2.php") { /* if is the 2 part of the home */
              set beresp.ttl = 5m;      /* The cache time for this fragment is 5 minutes */
      }
 ...
 }

So for apply this changes, you only need reboot the varnish and visit your page. When finish, you must view next result:

1
2
3
4
5
6
<html>
  <body>
    <p>2011-02-01 10:12:15</p>
    <p>2011-02-01 10:12:15</p>
  </body>
</html>

But if wait a minute and refresh the page, you must view the next result:

1
2
3
4
5
6
<html>
  <body>
    <p>2011-02-01 10:13:15</p>
    <p>2011-02-01 10:12:15</p>
  </body>
</html>

And if wait 5 minutes and refresh the page, the 2 dates are changed.

And that’s all. This is a simple example that how implement ESI in your web page. Note that the main file is index.html and not index.php (so that works with index.php too). I wanted to make this example with index.html for demonstrate that Apache is not that compile and execute the ESI language, Varnish is who make it. If you access directly to Apache and execute the code, probably you must view the <esi> marks, but if you access with the varnish, the web is showed correctly.

I think that this mark up language has got more potential and I will try continue research with it. If anybody have interest for this technology and want to do joint tests, feel free to contact me through the comments on this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *