Varnish is an HTTP accelerator. Its use will allow you to server more content/requests with the same or even less hardware. But with all good things, for each use,the complexity of your solution will increase.
On this talk, I will show how to use Varnish to implement some advanced cache solutions like adding GeoIP processing on varnish and ESI (Edge Side Includes).
2. Who am i
●
●
●
Luis Ferro
Lead Developer in Rewards and Loyalty
Programs (Metropolis International)
When not coding, you can find me shooting
arrows or cooking, mostly...
http://www.linkedin.com/profile/view?id=73429631
3. Varnish before Hell Entrance
●
“Varnish is an HTTP accelerator designed for
content-heavy dynamic web sites. Varnish is
focused exclusively on HTTP.” - wikipedia
●
https://www.varnish-cache.org
●
Installing (debian/ubuntu)
sudo apt-get update
sudo apt-get install varnish
4. st
1 Circle - basics
●
Simple caching
●
Varnish, NGinX and PHP should be working
●
Default configurations apart from the ports
which should be:
Port 80 – Varnish, with backend to 8080
Port 8080 – NGinX, with backend to 9000
Port 9000 – PHP
6. st
1 - Checking it in - NGinX
NGinX Test
ab -n 10000 -c 10 http://127.0.0.1:8080/index.php
Requests per second:
253.01 [#/sec] (mean)
Time per request:
39.524 [ms] (mean)
Time per request:
3.952 [ms] (mean, across all concurrent requests)
Transfer rate:
29931.75 [Kbytes/sec] received
7. st
1 - Checking it in - Varnish
Varnish Test
ab -n 10000 -c 10 http://127.0.0.1:80/index.php
Requests per second:
2624.59 [#/sec] (mean)
Time per request:
3.810 [ms] (mean)
Time per request:
0.381 [ms] (mean, across all concurrent requests)
Transfer rate:
311418.96 [Kbytes/sec] received
10x FASTER
With almost default config
8. nd
2 Circle – The path to cache
●
Add hit / miss information to all requests
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}
10. rd
3 Circle – Force march
●
Force Refresh
sub vcl_recv { [...]
if (req.http.Cache-Control ~ "no-cache") {
ban_url(req.url);
}
[…] }
●
You may want to protect / restrict who can call
this “ban”
12. rd
4 Circle – Expired content
●
Add php headers to force content to expire after
a certain quantity of seconds
function headers_for_page_cache($cache_length=600){
$cache_expire_date = gmdate("D, d M Y H:i:s", time() + $cache_length);
header("Expires: $cache_expire_date");
header("Pragma: cache");
header("Cache-Control: max-age=$cache_length, s-maxage=$cache_length");
header("User-Cache-Control: max-age=$cache_length");
}
14. th
5 Circle – All in the bag
●
Ensure cache by delete cookies on unwanted
items (images, css, js)
sub vcl_recv { […]
if (req.url ~ ".(png|gif|jpg)$") {
remove req.http.Cookie;
}
[...]}
●
Varnish doesn't cache anything with a cookie!
16. th
6 Circle – Watch your steps
●
Install GeoIp
●
Get GeoipUsingInlineC from:
https://www.varnish-cache.org/trac/wiki/GeoipUsingInlineC
●
Compile and install it (findable by your OS)
20. th
7 Circle – Know your enemies
●
Device Detection
●
Download the devicedetection.vcl from:
http://github.com/varnish/varnish-devicedetect/
●
Add it to default.vcl
sub vcl_recv { […]
call detectdevice;
[…] }
22. th
8 Circle – Tongue in cheek
●
Accepted language
●
Download and create the accept-language.vcl from:
http://github.com/cosimo/varnish-accept-language
●
Include the generated file and call it in default.vcl
sub vcl_recv { […]
C{
vcl_rewrite_accept_language(sp);
}C
[…] }
24. th
9 Circle – Divide and conquer
●
To activate add in your default.vcl
sub vcl_fetch { [...]
if( beresp.http.esi-enabled == "1") {
set beresp.do_esi = true; /* Do ESI processing */
set beresp.ttl = 120s; /* Sets the TTL on the HTML
above */
unset beresp.http.esi-enabled;
} [...]
25. th
9 - Check
Remember, ESI has issues with non-xml like files. So, the file where you
place the <esi: instructions have to look like a xml file.
26. More to come... if there was time...
●
Redirects
●
ESI – Sessions / Users
●
Purge
●
SSL / SPDY / Compression
●
Load Balancing
●
Webserver (O.o really?)
28. Thank you for listening and...
●
●
●
… Specially to varnish-software and everyone that
somehow contributed directly to enrich varnish.
Always check varnish-software website in case of doubt,
there is an huge amount of little tricks there.
Remember: using varnish, is good for the environment
(less power = win)!
29. Credits and Thanks
●
Botticelli Chart of Dante's Hell
http://www.flickr.com/photos/distan/3708997567/
by Dina Dubrovskaya (Real Distan)
●
The great people that made Varnish possible
●
PHP London for accepting this talk