This bunch of files should help you to setup a Proof of Concept (POC) about Varnish cache and ESI.
It has been tested with varnish-3.0.2 on Mac OSX 10.7.
If not already done, you need to install :
- Apache2
- PHP5
- Varnish
-
Clone the project !
git clone git@github.com:tomav/Varnish-ESI-Example.git -
Edit your
/etc/hosts
Add a new line127.0.0.1 varnish-esi-example -
Edit
Varnish-ESI-Example/apache/varnish-esi-example.conf
Adapt theDocumentRootandCustomLogto matchVarnish-ESI-Example/wwwfolder located in the package. -
Load the backend VirtualHost in Apache
sudo ln -s /this/should/be/replaced/to/match/Varnish-ESI-Example/apache/varnish-esi-example.conf /etc/apache2/sites-enabled/
sudo apachectl restart -
Edit Varnish configuration file
Varnish-ESI-Example/varnish/varnish-esi-example.vcl
Adapt/this/should/be/replaced/to/match/occurrences to match files located inVarnish-ESI-Example/varnishfolder. -
Start Varnish (but adapt the command line before, like in previous step)
sudo varnishd -a varnish-esi-example:8080 -T localhost:6082 -f /this/should/be/replaced/to/match/Varnish-ESI-Example/varnish/varnish-esi-example.vcl -s file,/opt/local/var/varnish/varnish-esi-example.bin,10M
Your static website should be available at http://varnish-esi-example/. It uses PHP includes to load lorem.php and footer.php, and the page is served by Apache. (Varnish is not used at this time, so no cache).
Your proxied website should be available at http://varnish-esi-example:8080/. Same as above, but received by Varnish, and processed by Apache. PHP includes are used, no ESI yet. Again, no cache at this time. (not used in the tests)
Your cached website should be available at http://varnish-esi-example:8080/?esi. Here, ESI is used and cache is used on lorem.php and footer.php files. The first file contains a sleep(1) PHP instruction to simulate a 1 second Time To First Byte (TTFB). The second is cached 24 hours.
Your full cached website should be available at http://varnish-esi-example:8080/?full. Here, ESI is used and cache is used on all page includes.
The index.php file contains :
- a
meta-refreshto let you see what is changing every 2 seconds - the time appears in the center and right column. You will see that
/lorem.phpis cached 10 seconds via ESI. - the
/footer.phpfile is cached 24 hours but you can purge it manually via a link which call/purge.php(modify the text of the file, then click "purge").
Note : The cache Time To Live (TTL) is configurable in varnish-esi-example-fetch.vcl for both lorem.php and footer.php files.
Now, play with ab and check Varnish-ESI-Example/www/varnish-esi-example-access_log to see when Apache is reached.
The following tests are focused on PHP files, not statics like JS/CSS. 1000 hits with 10 concurrent queries.
ab -n 1000 -c 10 http://varnish-esi-example/
Requests per second: 9.97 [#/sec] (mean) (5 fails)
127.0.0.1 - - [05/Apr/2012:11:51:33 +0200] "GET / HTTP/1.0" 200 2750
127.0.0.1 - - [05/Apr/2012:11:51:34 +0200] "GET / HTTP/1.0" 200 2750
127.0.0.1 - - [05/Apr/2012:11:51:34 +0200] "GET / HTTP/1.0" 200 2750
127.0.0.1 - - [05/Apr/2012:11:51:34 +0200] "GET / HTTP/1.0" 200 2750
127.0.0.1 - - [05/Apr/2012:11:51:34 +0200] "GET / HTTP/1.0" 200 2750
(~2 minutes later, last hit.)
127.0.0.1 - - [05/Apr/2012:11:53:13 +0200] "GET / HTTP/1.0" 200 2750
Apache & Varnish : cache on lorem.php & footer.php via ESI (except first hit), layout served by Apache
ab -n 1000 -c 10 http://varnish-esi-example:8080/\?esi
Requests per second: 622.81 [#/sec] (mean) (no fail)
127.0.0.1 - - [05/Apr/2012:11:48:36 +0200] "GET /?esi HTTP/1.1" 200 1586 # The page
127.0.0.1 - - [05/Apr/2012:11:48:36 +0200] "GET /lorem.php HTTP/1.1" 200 1268 # Its content
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /footer.php HTTP/1.1" 200 66 # Its footer
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
127.0.0.1 - - [05/Apr/2012:11:48:37 +0200] "GET /?esi HTTP/1.1" 200 1586
(less than 1 second later, last hit)
127.0.0.1 - - [05/Apr/2012:11:48:38 +0200] "GET /?esi HTTP/1.1" 200 1586
ab -n 1000 -c 10 http://varnish-esi-example:8080/\?full
Requests per second: 892.39 [#/sec] (mean) (no fail)
127.0.0.1 - - [05/Apr/2012:10:49:45 +0200] "GET /?full HTTP/1.1" 200 2750
(only 1 hit on Apache, Varnish cached the 999 other page hits)