Varnish

Post on 11-May-2015

1.247 views 1 download

description

This presentation was done by Alexander Kuznecov, our main System Administrator about Varnish Caching during Drupal Camp Kiev 2012.

Transcript of Varnish

Varnish

VarnishTips and Tricks

Varnish

Clients Webserver

Request

HTTP-accelerator

Varnish

Clients Webserver

Request

Request

Request

HTTP-accelerator

Varnish

Clients Webserver

Request

Request

Request

Fault

HTTP-accelerator

Varnish

HTTP-accelerator

Clients Webserver

Request

Request

Request

Varnish Request

SubsequentInitial

Varnish

Flow start

recv

pass lookup

miss hit

deliver

done

fetch pipe

error

hash

Varnish

ESIPage

ESIblock

content

TTL: 60m

ESIblock

TTL: 5m

TTL: 0

Varnish

ESI

<esi:include src="/esi/region/123" />

Varnish

VMODsEssential Varnish Modules

* EXTEND FUNCTIONALITY

* DISTRIBUTION BY SOURCE CODE (C)

Varnish

Tips and Tricks

Varnish

Drupal: cache anonymousreq.http.Cookie

recv

SESS(.{32})=([^;]*)cookie

pass

Varnish

Drupal: cache anonymousreq.http.Cookie

recv

SESS(.{32})=([^;]*)cookie

pass

Varnish

Drupal: cache bothreq.http.Cookie

recv

customcookie

pass

Varnish

Tools * varnishadm

* varnishtop

* varnishstat

* varnishlog* varnishnsca

* varnishd -C -f /path/to/vcl

Varnish

Cache Invalidation

varnishadm

wget / curl(PURGE HTTP method)

+ regular expressions- access to terminal

+ remote call- single URL

Varnish restart- full cache invalidation- permissions- downtime

Varnish

Mobile User-Agentsreq.http.user-agent

recv

User Agentcondition

redirecthash

Varnish

Backendsbackend default {

.host = "192.168.0.1";

.port = "80"; }

backend other {.host = "192.168.0.33";.port = "80";

}

Varnish

ACLacl internal {

"127.0.0.1";"localhost";

}

acl admins {"192.168.1.1";"192.168.2.0/24";! "192.168.2.88";

}

Varnish

Virtual hosts

include "/etc/varnish/virtual-hosts/example.com.vcl";

sub vcl_recv {

if (req.http.Host ~ "^example.com") {

call explcom_vcl_recv; }}

Varnish

HTTP-authentication

HTTPauth

recv

pass

Varnish

HTTP-authentication

!HTTP auth header

recv

error

req.http.Authorization

Varnish

HTTP-authentication

if (! req.http.Authorization ~ "Basic Zm9vOmJhcgo="){ error 401 "Restricted";}

$ echo -n "foo:bar" | base64

Varnish

Errorif (obj.status == 400) {

set obj.http.Content-Type = "text/html; charset=utf-8";synthetic {"

<html> <head> <title>400 Bad request</title> </head> <body> <h1>Error 400 Bad request</h1> <p>Bad request</p> </body> </html>"};

return (deliver);}

Varnish

Custom error pages

* Strict HTML

* Inline C

* Combined

Methods:

Varnish

Redirects

ERROR

Varnish

Redirects recv

error

condition

customerror code

error codeaction

Varnish

GeolocationVMOD «GeoIP»

set req.http.X-Country-Code = geoip.client_country_code();

recv

GeoIP headercondition

restrictaccess

redirect

Varnish

Custom TTL

set beresp.ttl = 10m;

if (beresp.http.X-TTL) {C{char *ttl;ttl = VRT_GetHdr(sp, HDR_BERESP, "\06X-TTL:");VRT_l_beresp_ttl(sp, atoi(ttl));}C

}

Varnish

Questions ?

Varnish

Alexander Kuznecov

akuznecov@adyax.com