Varnish
Making eZ Publish sites fly




         Peter Keung


                       http://www.mugo.ca
eZ DFS


Web    Web   Web




NFS            MySQL




                   http://www.mugo.ca
Predictably unpredictable




                  http://www.mugo.ca
Varnish: What
Reverse proxy

              1. request               2. MISS
 Visitor #1                 Varnish                  Web app

              4. response             3. response




                                        Web app
              1. request
 Visitor #2                 Varnish
                                          zzzzzz
                                         zzzzzzz
                2. HIT                     zzzzz




                                       http://www.mugo.ca
Varnish: What

https://www.varnish-cache.org/
Example: Varnish on port 80; Apache on port
88
Serve HTTP GET requests: HTML and assets
very very very very very fast
Can sit on each web server
Can be a load balancer
Saint and grace modes

                                 http://www.mugo.ca
Varnish: Why
Traffic spikes!
vs more hardware
  Cost
  Reliance on NFS
vs CDN
  Cost
  Closer to the application, more hands on
  Not closer to the user
  Use together

                                  http://www.mugo.ca
Optimize everywhere

Apache, MySQL, eZ Publish
Browser caching
Minifying CSS and JS




                            http://www.mugo.ca
Varnish + eZ Publish
eZ Market extension
  http://ez.no/Products/eZ-Market
Mugo Varnish extension:
  https://github.com/mugoweb/mugo_varnish




                                    http://www.mugo.ca
How to integrate
Custom static cache handler
  https://github.com/ezsystems/ezpublish/pull/119
  eZ Publish 2011.9 or 4.7
ezpEvent since 4.5
Purge specific URLs in Administration Interface




                                  http://www.mugo.ca
Configuring Varnish
VCL
 Similar to C
 vcl_recv: what to do with the request
 vcl_hit / vcl_miss
 vcl_fetch: what to do with the response
 vcl_deliver: send the response back
TTL



                                  http://www.mugo.ca
vcl_recv: purge
if( req.request == "PURGE" )
{
    # Limit access for security reasons
    if( !client.ip ~ purge )
    {
        error 405 "Not allowed.";
    }
    # URL purges -- one for the URL and one for all view parameter variations
    if( req.http.X-Purge-Url )
    {
        set req.http.X-Purge-Url1 = "^" + req.http.X-Purge-Url + "$";
        set req.http.X-Purge-Url2 = "^" + req.http.X-Purge-Url + "/(";
        ban( "obj.http.x-url ~ " + req.http.X-Purge-Url1 );
        ban( "obj.http.x-url ~ " + req.http.X-Purge-Url2 );
        error 200 "URL Purged.";
    }
    # Any regular expressions here
    if( req.http.X-Purge-Reg )
    {
        ban( "obj.http.x-url ~ " + req.http.X-Purge-Reg );
        error 200 "Regular Expression Purged.";
    }
    error 405 "Missing X-Purge-Url or X-Purge-Reg header.";
}

                                                            http://www.mugo.ca
vcl_fetch: setting TTL
# Only cache www.yoursite.com
# Only 200 responses
if( req.http.host == "www.yoursite.com" &&
    beresp.status == 200
)
{
    if( req.url ~ "^/var/plain/storage/images/.*" )
    {
        set beresp.ttl = 30d;
        set beresp.http.X-Ttl = "30d";
    }
    else
    {
        # Default caching time
        set beresp.ttl = 300s;
        set beresp.http.X-Ttl = "300s";
    }
}




                                                      http://www.mugo.ca
vcl_deliver: adding diagnostic info
set resp.http.X-Served-By = server.hostname;
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";
}




                                               http://www.mugo.ca
Tweaks
Cookies: eZ Publish < 4.4
Controlling TTL and excluding pages
  header( 'Edge-control: cache-maxage=86400s' );
  {ezpagedata_append( 'http_headers', 'Edge-
  Control: cache-maxage=0s' )}
Edge Side Includes




                                 http://www.mugo.ca
Ongoing maintenance
   varnishstat
   varnishadm
   curl -I http://www.yoursite.com
HTTP/1.1 200 OK
Server: Apache
X-Cache-Hits: 17
X-Cache: HIT
X-Served-By: CloudWeb2
Via: 1.1 varnish
X-Ttl: 300s
X-Powered-By: eZ Publish
Age: 206




                                     http://www.mugo.ca
Relax




Questions? hi@mugo.ca


                        http://www.mugo.ca

Varnish: Making eZ Publish sites fly

  • 1.
    Varnish Making eZ Publishsites fly Peter Keung http://www.mugo.ca
  • 2.
    eZ DFS Web Web Web NFS MySQL http://www.mugo.ca
  • 3.
    Predictably unpredictable http://www.mugo.ca
  • 4.
    Varnish: What Reverse proxy 1. request 2. MISS Visitor #1 Varnish Web app 4. response 3. response Web app 1. request Visitor #2 Varnish zzzzzz zzzzzzz 2. HIT zzzzz http://www.mugo.ca
  • 5.
    Varnish: What https://www.varnish-cache.org/ Example: Varnishon port 80; Apache on port 88 Serve HTTP GET requests: HTML and assets very very very very very fast Can sit on each web server Can be a load balancer Saint and grace modes http://www.mugo.ca
  • 6.
    Varnish: Why Traffic spikes! vsmore hardware Cost Reliance on NFS vs CDN Cost Closer to the application, more hands on Not closer to the user Use together http://www.mugo.ca
  • 7.
    Optimize everywhere Apache, MySQL,eZ Publish Browser caching Minifying CSS and JS http://www.mugo.ca
  • 8.
    Varnish + eZPublish eZ Market extension http://ez.no/Products/eZ-Market Mugo Varnish extension: https://github.com/mugoweb/mugo_varnish http://www.mugo.ca
  • 9.
    How to integrate Customstatic cache handler https://github.com/ezsystems/ezpublish/pull/119 eZ Publish 2011.9 or 4.7 ezpEvent since 4.5 Purge specific URLs in Administration Interface http://www.mugo.ca
  • 10.
    Configuring Varnish VCL Similarto C vcl_recv: what to do with the request vcl_hit / vcl_miss vcl_fetch: what to do with the response vcl_deliver: send the response back TTL http://www.mugo.ca
  • 11.
    vcl_recv: purge if( req.request== "PURGE" ) { # Limit access for security reasons if( !client.ip ~ purge ) { error 405 "Not allowed."; } # URL purges -- one for the URL and one for all view parameter variations if( req.http.X-Purge-Url ) { set req.http.X-Purge-Url1 = "^" + req.http.X-Purge-Url + "$"; set req.http.X-Purge-Url2 = "^" + req.http.X-Purge-Url + "/("; ban( "obj.http.x-url ~ " + req.http.X-Purge-Url1 ); ban( "obj.http.x-url ~ " + req.http.X-Purge-Url2 ); error 200 "URL Purged."; } # Any regular expressions here if( req.http.X-Purge-Reg ) { ban( "obj.http.x-url ~ " + req.http.X-Purge-Reg ); error 200 "Regular Expression Purged."; } error 405 "Missing X-Purge-Url or X-Purge-Reg header."; } http://www.mugo.ca
  • 12.
    vcl_fetch: setting TTL #Only cache www.yoursite.com # Only 200 responses if( req.http.host == "www.yoursite.com" && beresp.status == 200 ) { if( req.url ~ "^/var/plain/storage/images/.*" ) { set beresp.ttl = 30d; set beresp.http.X-Ttl = "30d"; } else { # Default caching time set beresp.ttl = 300s; set beresp.http.X-Ttl = "300s"; } } http://www.mugo.ca
  • 13.
    vcl_deliver: adding diagnosticinfo set resp.http.X-Served-By = server.hostname; 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"; } http://www.mugo.ca
  • 14.
    Tweaks Cookies: eZ Publish< 4.4 Controlling TTL and excluding pages header( 'Edge-control: cache-maxage=86400s' ); {ezpagedata_append( 'http_headers', 'Edge- Control: cache-maxage=0s' )} Edge Side Includes http://www.mugo.ca
  • 15.
    Ongoing maintenance varnishstat varnishadm curl -I http://www.yoursite.com HTTP/1.1 200 OK Server: Apache X-Cache-Hits: 17 X-Cache: HIT X-Served-By: CloudWeb2 Via: 1.1 varnish X-Ttl: 300s X-Powered-By: eZ Publish Age: 206 http://www.mugo.ca
  • 16.
    Relax Questions? hi@mugo.ca http://www.mugo.ca