1. Varnish
Varnish is a transparent, hard, protective finish or film primarily used in wood finishing but also for
other materials. Varnish is traditionally a combination of a drying oil, a resin, and
a thinner or solvent. Varnish finishes are usually glossy but may be designed to produce satin or
semi-gloss sheens by the addition of "flatting" agents. Varnish has little or no colour, is
transparent, and has no added pigment, as opposed to paints or wood stains, which contain
pigment and generally range fromopaque to translucent. Varnishes are also applied over wood
stains as a final step to achieve a film for gloss and protection. Some products are marketed as a
combined stain and varnish.
Christian Nölle 1
8. Varnish =
(HTTP|web) (proxy|accelerator)
Christian Nölle 8
9. Sag JA zu Performance!
Varnish works on 32-bit, but was designed for
64bit. It's all about virtual memory: Things like
stack size suddenly matter on 32bit. If you
must use Varnish on 32-bit, you're somewhat
on your own. However, try to fit it within 2GB.
I wouldn't recommend a cache larger than
1GB, and no more than a few hundred
threads... (Why are you on 32bit again?)
http://kly.no/posts/2010_01_26__Varnish_best_practices__.html
Christian Nölle 9
10. Server
64bit OS
Multi core
Speicher
Christian Nölle 10
11. Speicher satt?
-s malloc,5G
Öh, Speicher?
-s file,/tmp/varnish_storage.bin,5G
Lauschen lassen!
-a 0.0.0.0:80
Christian Nölle 11
12. Tuning?
After working with Varnish I've realized that you
do not really need to tune Varnish much: The
defaults are tuned. The only real exception
I've found to this is number of threads and
possibly work spaces.
http://kly.no/posts/2010_01_26__Varnish_best_practices__.html
Christian Nölle 12
13. Konfigurieren!
Varnish Configuration Language
VCL
/etc/varnish/default.vcl
-f /etc/varnish/meine.vcl
Christian Nölle 13
14. Definiert die Caching-Regeln
Wird geparst, in C compiliert und in
Varnish gelinkt!
Fallback auf default VCL
Christian Nölle 14
15. And Now for Something Completely
Different
Christian Nölle 15
16. Ich distanziere mich von der nun
folgenden Darsstellung:
Quelle: commons.wikimedia.org / Creative Commons CC0 1.0 Universal Public Domain Dedication
Christian Nölle 16
17. Was geht?!
vcl_recv
vcl_pipe
vcl_hash vcl_pass
vcl_miss
vcl_hit
vcl_fetch
vcl_deliver
Christian Nölle 17
23. Directors
director test round-robin { • Definition eines
{ .backend = server1; } Directors
{ .backend = server2; } • round-robin oder
{ .backend = { random
.host = "12.34.56.78";
• random mit
.port = "82";
Gewichtung: .weight
}
• Sowie
}
hash, client, fallback
}
und dns Directors
Christian Nölle 24
24. Grace mode
sub vcl_recv { • Objekt veraltet, aber
if (req.backend.healthy) { noch im Cache
set req.grace = 30s; • Grace mode: Varnish
} else { nutzt ein solches Objekt
set req.grace = 24h;
• Bedingungen:
}
– Existenz -> beresp.grace
}
– Erlaubnis -> req.grace
sub vcl_fetch {
set beresp.grace = 24h;
}
Christian Nölle 25
25. Übung ;)
• Was geht wann?
• 50 Sekunden?
– normale Auslieferung
• 63 Sekunden?
– Cache miss, aber grace
set beresp.ttl=1m; möglich
set req.grace = 30s; • 80 Sekunden?
– Cache miss, aber grace
set beresp.grace = 1h; möglich
• 94 Sekunden?
– Cache miss, grace nicht
erlaubt
• 3660 Sekunden (1H + 1m)
– Objekt wird aus Cache
entfernt!
Christian Nölle 26
26. Start mit einer .vcl von moc.net
Anpassung an varnish 3.0
Erfahrung gesammelt
EINIGES geändert!
;)
Christian Nölle 27
27. Einiges Wenig an TYPO3 Kram
sub vcl_recv { • Logins direkt durch die
if (req.url ~ Pipe schicken
"/typo3/index.php$") {
• Aufruf von vcl_pipe
return (pipe);
}
• Backends nicht cachen
if(req.url ~ "/typo3/") {
return (pass); • Aufruf von vcl_pass
}
}
Christian Nölle 28
28. sub vcl_fetch {
if (beresp.http.cache-control ~ "(no-cache|private)" ||
beresp.http.pragma ~ "no-cache") {
set beresp.ttl = 0s;
#Make sure that We remove alle cache headers, so the Browser does not cache it for us!
remove beresp.http.Cache-Control;
remove beresp.http.Expires;
remove beresp.http.Last-Modified;
remove beresp.http.ETag;
remove beresp.http.Pragma;
return (deliver);
}
}
• Was tun, wenn Seiten IP basiert ausgeliefert werden?
• vcl_fetch veranlassen, sowas nicht zu cachen!
Christian Nölle 29
29. • Fehlerseiten cachen ist
ne blöde Idee!
• Also direkt durch?
– Direkt wäre pass oder
sub vcl_fetch { gar pipe!
if (beresp.status >= 400) { • hit_for_pass = Pass in
return (hit_for_pass); fetch!
}
• Request pile up wird
}
vermieden
Christian Nölle 30
30. • Objekte je nach obj.hits gestaffelt länger cachen
• Redakteure sollen einzelne Seiten aus dem Cache
aktualisieren können durch STRG & F5.
acl refresh {
„123.456.0.0"/16;
}
sub vcl_hit {
if (obj.hits == 500) {
set obj.ttl = 1d;
} elsif (obj.hits == 10000) {
set obj.ttl = 3d;
}
#internal hit vcl for CTRL F5
if (req.http.pragma ~ "no-cache" || req.http.Cache-Control ~ "no-cache") {
if (client.ip ~ refresh) {
purge;
return (pass);
}
}
}
Christian Nölle 31
32. Geräteerkennung durch varnish
vcl_hash für verschiedene Caches je
Gerätegruppe
TYPO3 liefert je nach X-Header
verschiedene Templates.
Christian Nölle 33
33. Geräteerkennung durch varnish
vcl_hash für verschiedene Caches je
Gerätegruppe
TYPO3 liefert je nach X-Header
verschiedene Templates.
Christian Nölle 34
34. Besser:
https://github.com/varnish/varnish-
devicedetect
Christian Nölle 35
35. Auschnitte der Erkennungsroutine
include "devicedetect.vcl"; • fertige Routine
sub vcl_recv { call inkludieren
devicedetect; }
• Aufrufen in vcl_recv
unset req.http.X-UA-Device;
set req.http.X-UA-Device = • Setzt http X-UA-Device
"pc"; Header
if (req.http.Cookie ~ "(?i)X- • Cookie zum Abschalten
UA-Device-force") {
möglich!
…
}
Christian Nölle 36
36. sub vcl_fetch {
if (req.http.X-UA-Device) {
if (!beresp.http.Vary) { # no Vary at all
set beresp.http.Vary = "X-UA-Device";
} elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
}
}
# comment this out if you don't want the client to know your classification
set beresp.http.X-UA-Device = req.http.X-UA-Device;
}
• Prüft und ergänzt den X-Header
• und nutzt Vary um den Content zu unterscheiden
• Purging besser möglich als mit vcl_hash
Christian Nölle 37
37. sub vcl_deliver {
if ((req.http.X-UA-Device) && (resp.http.Vary)) {
set resp.http.Vary = regsub(resp.http.Vary, "X-UA-
Device", "User-Agent");
}
}
• der Vary Header muss allerdings auf dem Weg zum
Client zurück wieder normalisiert werden.
• Gefahr: Andere Caches da draussen…
Christian Nölle 38
38. Quelle: Bob Smith, http://sxc.hu/browse.phtml?f=profile&l=brainloc
Christian Nölle 39
39. Aus der Doku…
Varnish will, in the default
configuration, not cache a object
coming from the backend with a Set-
Cookie header present. Also, if the
client sends a Cookie header, Varnish
will bypass the cache and go directly
to the backend.
Christian Nölle 40