Drupalcamp gent-2012 - varnish for dummies

Preview:

DESCRIPTION

My drupalcamp Gent slideshow for varnish for drupal.varnish is a reverso proxy cache or web accelerator.- Brief explenation of caching mechanisms- what is and what does varnish- code examples of loadbalancing- code examples of caching improvements- code examples of caching for drupal- code examples of caching for mobile- how to do edge side includes Follow me on twitter: drupal_sensei

Citation preview

1.

Varnish for Dummies Frederik Wouters

2.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

3.

Introduction

Frederik Wouters •  2010 - 05/2012 Amplexor •  05/2012 - ? Self employed at Wieni

Drupal Solr Varnish Apache Lighttpd Jquery …

4.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

5.

Introduction

Opcode caching

Database Caching

User Storage caching

Reverse proxy Caching Loadbalancing

Client cache

Proxy Caching Client network

DMZ

Webserver

6.

Opcode Caching

Opcode cache mechanisms preserve the generated code in cache so that it need only be generated a single time to server hundreds or millions of subsequent requests. Enabling opcode cache will reduce the time it takes to generate a page by up to 90%. !

Adv? Php code executed a lot

7.

Opcode Caching - Alternatives

Alternative PHP Cache (APC) eAccelerator ionCube PHP Accelerator Turck MMCache XCache Nusphere PhpExpress Zend Optimizer+ Zend Platform Windows Cache Extension for PHP!

8.

Query Caching

You can setup that up in /etc/my.cnf (Red Hat) or /etc/mysql/my.cnf (Debian) file: # vi /etc/my.cnf!

Append config directives as follows:

query_cache_size = 268435456!

query_cache_type = 1!

query_cache_limit = 1048576!

Adv? many (read) queries

9.

User Storage Caching

Inside The CMS -  Drupal caching -  Boost -  Expire module -> Use expensive apache memory -> Use expensive database connections

Adv? Global Data used a lot Over mutliple requests ( > x/req )

Outside the CMS -  Memcache -  Redis -  MongoDB

10.

Proxy Caching

Happens at the client (network) side. Reduces Outgoing traffic Apache does proxy caching Squid is good

Adv? files/requests that are often used

Client network

11.

Reverse Proxy Caching

Happens at the server side. Reduces incoming traffic Also called web-accellerator

Adv? files/requests that are often used

!   Drupal(on apache) Response Time : 500 ms !   Varnish Response Time : 50 ms !   Capacity multiplies by x

DMZ

12.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

13.

Content Delivery Network

Return files from servers located in your area

Adv? Speed

14.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

15.

Varnish

!   Drupal(on apache) Response Time : 500 ms !   Varnish Response Time : 50 ms !   Server load î = Capacity ì = #WIN

16.

Varnish

17.

Varnish

18.

Varnish - installation

Installation "Ubuntu = apt-get install varnish!!Redhat = yum install varnish!!Suse = package available (at your own risk)!!Mac = sudo port install varnish!!Also available on Mac/(open)solaris/FreeBSD/!!Windows(Cygwin)!!

19.

Varnish - installation

Configuration - Basics!"# file /etc/default/varnish!#Default http port = 6081 !#Default console port = 6082!#I Prefer Port 80.!!DAEMON_OPTS="-a :6081 80 \! -T localhost:6082 \! -f /etc/varnish/default.vcl \! -S /etc/varnish/secret \! -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"!!!!

20.

Varnish - installation Configuration – Advanced!# file /etc/default/varnish!!VARNISH_VCL_CONF=/etc/varnish/default.vcl!VARNISH_MIN_THREADS=1!VARNISH_MAX_THREADS=5000!VARNISH_THREAD_TIMEOUT=120!KINEPOLIS_VARNISH_STORAGE="malloc,4G”!KINEPOLIS_CONNECT_TIMEOUT="connect_timeout=600 VARNISH_TTL=900 !DAEMON_OPTS="-a ${VARNISH_LISTEN_ADDRESS}:${VARNISH_LISTEN_PORT} \! -f ${VARNISH_VCL_CONF} \! -T ${VARNISH_ADMIN_LISTEN_ADDRESS}:${VARNISH_ADMIN_LISTEN_PORT} \! -t ${VARNISH_TTL} \! -w ${VARNISH_MIN_THREADS},${VARNISH_MAX_THREADS},${VARNISH_THREAD_TIMEOUT} \! -u varnish -g varnish \! -s ${KINEPOLIS_VARNISH_STORAGE} \! -p ${KINEPOLIS_CONNECT_TIMEOUT} \! -p thread_pools=2 \! -p thread_pool_min=200 \! -p thread_pool_max=5000 \! -p thread_pool_add_delay=2"!!!

21.

Varnish - installation Configuration – Ready!#Start your engines!Sudo /etc/init.d/varnish start|restart!!!!#The site is already Live? Do some hotswapping:!!#login to the varnish admin console!varnishadm -T localhost:6082!!#compile the vcl file!Vcl.load caching_vx /etc/varnish/default.vcl!!#Switch to new compiled vcl file!Vcl.use caching_vx!!#quit the admin console!quit!!! !!!!

22.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

23.

VCL Logic

!   The VCL syntax is very simple, and deliberately similar to C and Perl. Blocks are delimited by curly braces, statements end with semicolons, and comments may be written as in C, C++ or Perl according to your own preferences.

!   In addition to the C-like assignment (=), comparison (==, !=) and boolean (!, && and ||) operators, VCL supports both regular expression and ACL matching using the ~ and the !~ operators.

!   I will use syntax of varnish 2.1.x

https://www.varnish-cache.org/docs/2.1/reference/index.html https://www.varnish-cache.org/docs/3.0/reference/vcl.html

24.

Varnish – VCL Logic Backends!# file /etc/varnish/backends.vcl!

!backend default {! .host = "188.188.188.228";! .port = "80";!}!!backend static1 {! .host = "188.188.188.229";! .port = "80";!}!!backend static2 {! .host = "188.188.188.235";! .port = "80";!}!

!!

25.

Varnish - VCL Logic Backends!# file /etc/varnish/backends.vcl!!

backend default {! .host = "188.188.188.228";! .port = "80"; ! .connect_timeout = 1s;! .first_byte_timeout = 100s;! .between_bytes_timeout = 100s;!#In Varnish 2.0.4 the default value for #connect_timeout is 0.4s, first_byte_timeout #and between_bytes_timeout options is 60s.!!}!

!!

26.

Varnish - VCL Logic Backends!# file /etc/varnish/backends.vcl!

backend default {! .host = "188.188.188.228";! .port = "80"; ! .connect_timeout = 1s;! .first_byte_timeout = 100s;! .between_bytes_timeout = 100s;! .probe = { ! .url = "/status.php"; ! .interval = 20s; #time between the polls! .timeout = 10s; #time to finish request ! .window = 2; #amount of polls needed for healthy! .threshold = 2; #amount of window polls -> healthy! }!!}!https://www.varnish-cache.org/trac/wiki/BackendPolling!

!!

27.

Varnish - VCL Logic Backends - probe!# file /var/www/html/myapp/status.php!

!Status.php (or a drupal status url)!!!//Bootstrap!//DB test!//Slave DB test!//Memcache (connectivity) running!//files directory mounted!//return HTTP 200 OK!!!!http://it2servu.be/statusphp-drupal-7-varnish-steroids!

!

28.

Varnish - VCL Logic Loadbalancing = Backends = Director!# file /etc/varnish/backends.vcl!

!director web round-robin {! {! .backend = static1;! }! {! .backend = static2;! }! }!

#client!#set client.identity = client.ip;!#set client.identity = req.http.user-agent;!

#round Robin!#random!#fallback!

29.

Varnish – VCL Logic DRUPAL SETTINGS!# file sites/mysite/settings.php!!$conf['reverse_proxy'] = TRUE;!$conf['reverse_proxy_adresses'] = array('188.18.188.227');!$conf['cache_backends'] = !array('sites/all/modules/contrib/varnish/varnish.cache.inc'); !$conf['cache_class_cache_page'] = 'VarnishCache';!$conf['page_cache_invoke_hooks'] = FALSE; !$conf['cache'] = 1;!$conf['cache_lifetime'] = 21600; #think about this one!!$conf['page_cache_maximum_age'] = 10; !$conf['reverse_proxy_header'] = 'HTTP_X_FORWARDED_FOR';!$conf['omit_vary_cookie'] = TRUE;!!

!

30.

Varnish – VCL Logic Purging the cache from drupal!!<?php!if (module_exists('varnish')) { !

!module_load_include('module', 'varnish'); !

!#clear only the frontpage!!_varnish_terminal_run('url.purge ' . $base_url);!

!!#clear all Varnish cache!!_varnish_terminal_run('url.purge .’);!

!}!!#There’s a module that helps you with that.!http://drupal.org/project/cache_actions !!!!!

31.

Varnish – Flow

Let’s Cache

So much for the proxy/loadbalancer

���Variables req.* request reps.* response bereq* backend request obj.* request object client.* server.*

32.

33.

34.

35.

36.

Varnish – VCL Logic Configuration – VCL Logic!# file /etc/varnish/default.vcl!!include "/etc/varnish/backends.vcl";!#the most simple vcl_recv ever!sub vcl_recv {!

!set req.backend = web;

!#return (pipe);!! !#pipe from server

!!return (pass);!! !#fetch from server (no caching)!

!#return(lookup);! ! !# delivers from cache !}!

!

37.

Varnish – VCL Logic Vcl_deliver !#Called before a cached object is delivered to the client !#makes life a little easier.!!sub vcl_deliver {! !if (obj.hits > 0) {! ! !set resp.http.X-Cache = "HIT";! !} else {! ! !set resp.http.X-Cache = "MISS";! !}!! !set resp.http.X-Hits = obj.hits;!} !!

38.

Varnish - Debugging

39.

Varnish – VCL Logic Vcl_fetch #Called when the requested object has been retrieved #from the backend, or the request to the backend has #failed !sub vcl_fetch {!

!# Grace to allow varnish to serve content if backend is lagged!!set obj.grace = 5m;!

!!if (!beresp.cacheable) {!

!set beresp.http.X-Cacheable = "NO:Not Cacheable";! !return(pass); !

!} elsif (beresp.http.Cache-Control ~ "private") {! !set beresp.http.X-Cacheable = !

! ! ! ! !"NO:Cache-Control=private";! !return(pass);!

!} else {! ! !set beresp.http.X-Cacheable = "YES";!

!}!!return(deliver); !!

}!

40.

Varnish - Debugging

41.

Varnish – VCL Logic vcl_recv !#forward the client IP address for drupal!set req.http.X-Forwarded-For = client.ip; !#pipe abnormal request types!if (req.request != "GET" && req.request != "HEAD" &&! req.request != "PUT" && req.request != "POST" &&! req.request != "TRACE" && req.request != "OPTIONS" && req.request != "DELETE") {!

! return (pipe); #Non-RFC2616 or CONNECT!}!!#only GET and HEAD requests are cacheable!if (req.request != "GET" && req.request != "HEAD") !{!

!return (pass);!}!

!!

42.

Varnish – VCL Logic vcl_recv !#Drupal7 logged in users are passed!if (req.http.Cookie ~ "SESS[a-z0-9]+") {! return (pass);!}!!#in backends.vcl!acl internal {"188.188.188.0"/24;}!!#Deny access from IP’s outside the DMZ!if (req.url ~ "(cron|install|status)\.php$" && !client.ip ~ internal) {!

!error 404 "Access is denied";! !} !

43.

Varnish – VCL Logic vcl_recv !#normalize user agents (reduces cache entries)!if (req.http.user-agent ~ "MSIE") {!

!set req.http.user-agent = "MSIE";!} else {!

!set req.http.user-agent = "Mozilla";!} !# Normalize encoding request types (reduce cache entries)!if (req.http.Accept-Encoding) {! if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$") {! # No point in compressing these! remove req.http.Accept-Encoding;! } elsif (req.http.Accept-Encoding ~ "gzip") {! set req.http.Accept-Encoding = "gzip";! } elsif (req.http.Accept-Encoding ~ "deflate") {! set req.http.Accept-Encoding = "deflate";! } else {! # unkown algorithm! remove req.http.Accept-Encoding;! }!}!

44.

Varnish – VCL Logic vcl_recv !#Don’t cache movies(save memory) !if (req.http.host == "cd3.kinepolis.com” ||!

!req.url ~ "^/admin/content/backup_migrate/export”){ !!#CDN MODULE / backup and migrate!!return (pipe);!

}!!#cache the css/javascript (save speed) !!if (req.url ~ "(?i)\.(css|js)(\?[a-z0-9=\.]+)?$") {!

!unset req.http.Cookie; #reduce cache entries!!return (lookup);!

}!#cache and cleanup files !if (req.url ~ "(?i)\.(png|gif|jpeg|jpg|ico|swf|html|htm)(\?[a-z0-9=\.]+)?$") { !

! unset req.http.Cookie;!! return (lookup); #Or pass from dedicated server!

}!!!

45.

Varnish – VCL Logic vcl_recv !#ajax !if(!

!req.url ~ "^/admin/build/features" || #features!!req.url ~ "^/info/.*$" || !!req.url ~ "^/batch/.*$" || #batch API uses AJAX!!req.url ~ "^/flag/.*$" || #batch API uses AJAX!!req.url ~ "^.*/ajax/.*$" || #custom ajax paths!!req.url ~ "^.*/ahah/.*$" || #drupal ajax paths!!req.url ~ "^.*/admin/.*$" || #admin pages!!req.url ~ "^.*/user/.*$" || #user pages!!req.url ~ "^.*token.*$” #token paths !!!) {!! !# Don't cache, pass to Apache backend!

return (pass);!!}!

#Pay attention! Default drupal ahah uses POSTS = NO CACHING!#When possible GET or create your own paths w/wo “ajax”!!

46.

Varnish – VCL Logic Stale while revalidate!!Sub vcl_recv{!

if(!req.backend.healthy){!!set req.grace = 5m;!

}!set beresp.grace = 30m;!

}!!Sub vcl_fetch{!

!set req.grace = 15s;!}!

47.

Varnish – VCL Logic Stale if error!!Sub vcl_fetch{!

if(beresp.status == 500 ){! set beresp.saintmode = 10s;!!return (restart);!

}!set beresp.grace = 30m;!

}!

48.

Varnish – VCL Logic Purge a page!!Sub vcl_fetch{! if (req.request == "PURGE") {! if (client.ip ~ access) {! return(lookup);! }! }!}!sub vcl_hit {! if (req.request == "PURGE") {! set obj.ttl = 0s;! error 200 "Purged.";! }!}!!sub vcl_miss {! if (req.request == "PURGE") {! error 404 "Not in cache.";! }!}!

acl access{! "localhost";! "192.168.1.0"/24; ! !"192.168.1.23"; !}!

49.

Varnish – VCL Not good enough? Your own C Code!!!

#Just like that:!!C{! syslog(LOG_INFO, "Just served the xxxth page");!}C!!#because all is compiled to C code anyway.!!!

50.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Debugging !   Mobile !   ESI

51.

Debugging

!   Log files? !   Tools

¬  Firebug ¬  Varnishlog ¬  Varnishtop ¬  Varnishhist ¬  Varnishstat

52.

Varnish Debugging

! varnishlog ¬  The varnishlog utility reads and presents varnishd(1) shared memory

logs.

¬  Official documentation ¬  Varnishlog Cheat sheet

53.

Varnish - Debugging

54.

Varnish Debugging

! varnishtop ¬  The varnishtop utility reads the shared memory logs and presents a

continuously updated list of the most commonly occurring log entries. With suitable filtering using the -I, -i, -X and -x options, it can be used to display a ranking of requested documents, clients, user agents, or any other information which is recorded in the log.

¬  varnishtop -i rxurl will show you what URLs are being asked for by the client.

¬  varnishtop -i txurl will show you what your backend is being asked the most.

¬  varnishtop -i RxHeader -I Accept-Encoding will show the most popular Accept-Encoding header the client are sending you.

55.

56.

Varnish - Debugging

! varnishhist ¬  The varnishhist utility reads varnishd(1) shared memory logs and

presents a continuously updated histogram showing the distribution of the last N requests by their processing. The value of N and the vertical scale are displayed in the top left corner. The horizontal scale is logarithmic. Hits are marked with a pipe character (“|”), and misses are marked with a hash character (“#”). varnishsizes Varnishsizes does the same as varnishhist, except it shows the size of the objects and not the time take to complete the request.

¬  This gives you a good overview of how big the objects you are serving are.

57.

Varnish - Debugging

58.

Varnish - Debugging

! varnishstat ¬  Varnish has lots of counters. We count misses, hits, information about

the storage, threads created, deleted objects. Just about everything. ¬  varnishstat will dump these counters. This is useful when tuning

varnish. There are programs that can poll varnishstat regularly and make nice graphs of these counters. One such program is Munin. Munin can be found at http://munin-monitoring.org/ . There is a plugin for munin in the varnish source code.

¬  See this in your drupal site (after installing the varnish module)

59.

60.

!   Introduction !   Caching explained !   CDN? !   Varnish installation/configuration !   VCL Logic !   Drupal VCL rules !   Mobile !   ESI

61.

Varnish - Mobile

Depends on your strategy !   Webserver handles mobile !   Redirect to mobile site !   Responsive

62.

Varnish – Mobile Webserver uses user agent!!sub normalize_user_agent {! if (req.http.user-agent ~ "MSIE") {! set req.http.X-UA = "msie";! } else if (req.http.user-agent ~ "Firefox") {! set req.http.X-UA = "firefox";! } else if (req.http.user-agent ~ "Safari") {! set req.http.X-UA = "safari";! } else if (req.http.user-agent ~ "Opera Mini/") {! set req.http.X-UA = "opera-mini";! } else if (req.http.user-agent ~ "Opera Mobi/") {! set req.http.X-UA = "opera-mobile";! } else if (req.http.user-agent ~ "Opera") {! set req.http.X-UA = "opera";! } else {! set req.http.X-UA = "nomatch";! }!}!!sub vcl_recv {! !…!

!call normalize_user_agent;!!!!

63.

Varnish – Mobile Redirecting to mobile site!!#in vcl_recv!set req.http.X-Device = "desktop";!if (req.http.User-Agent ~ "iPad" || req.http.User-Agent ~ "iP(hone|od)" || req.http.User-Agent ~ "Android" || req.http.User-Agent ~ "SymbianOS" || req.http.User-Agent ~ "^BlackBerry" || req.http.User-Agent ~ "^SonyEricsson" || req.http.User-Agent ~ "^Nokia" || req.http.User-Agent ~ "^SAMSUNG" || req.http.User-Agent ~ "^LG") {!

!# Define smartphones, tablets and phones!!set req.http.X-Device = "mobile";!!error 750;!

}!!#In vcl_error !if (obj.status == 750){! !set obj.http.Location = "http://kinepolis.mobi"; !! !set obj.status = 302;! !return (deliver); !! }!!!!!

64.

Varnish – Mobile Responsive!!#simple optimization to increase cache hit ratio!!sub normalize_user_agent {!

!set req.http.X-UA = ”allthesame";!}!!sub vcl_recv {! !…!

!call normalize_user_agent;!!…!

}!!!!

65.

ESI

66.

Edge side includes

!   http://en.wikipedia.org/wiki/Edge_Side_Includes !   https://www.varnish-cache.org/docs/3.0/tutorial/esi.html !   http://drupal.org/project/esi

67.

Varnish – ESI How it works!!#index.html!<html>! <p>Varnish will work on this page: ! <esi:include src="/date.php" />.! </p>!</html>!!#date.php!<?php!echo date('Y-m-d');!!#default.vcl!sub vcl_fetch {! if (req.url == "/index.html") {! set beresp.do_esi = true;! }!}!!!!

68.

Varnish – ESI •  Enable Drupal Modules

•  ESI – Block •  ESI – Edge side includes

•  Edit the caching settings of the block

Look out for ESI_xmlerror records in the varnishlog output, they will tell you the bytenumber in the file, if trouble is found during the ESI parsing. !!

69.

Did it digest? Now’s the time to tell!

Wouters.f@gmail.com Drupal_sensei

70.

Feedback & follow-up: http://drupalcampgent.be/feedback