bephpug - varnish & co

50
bephpug http://www.bephpug.de 1 Freitag, 13. Mai 2011

Post on 22-Oct-2014

4.087 views

Category:

Technology


3 download

DESCRIPTION

01.06.2010 - http://wiki.pooteeweet.org/BEPHPUG/

TRANSCRIPT

Page 1: bephpug - varnish & co

bephpughttp://www.bephpug.de

1Freitag, 13. Mai 2011

Page 2: bephpug - varnish & co

varnish & coFlorian Holzhauer - @fholzhauerVortrag bephpug Juni 2010http://holzhauer.it

2Freitag, 13. Mai 2011

Page 3: bephpug - varnish & co

Disclaimer

„Bauchentscheidungen“

nicht totoptimiert

Aufwand / Nutzen

Es gibt meistens mehr als eine Lösung

lighttpd, nginx, $webserver

XCache, APC, eAccelerator

3Freitag, 13. Mai 2011

Page 4: bephpug - varnish & co

Überblick

Problem: LAMP-Maschine unter Last

varnish

Standard-Administration

fancy features

Optimierung für Faule: xCache, tuning-primer.sh

4Freitag, 13. Mai 2011

Page 5: bephpug - varnish & co

Beispiel: lawblog.de

Kein „echtes“ Kundenprojekt, trotzdem viel Last

Guter Kontakt, alle Adminfreiheiten

Testbed für neue Tools

Sehr gutes Besucher-Feedback

Klassische L(A)MP-Anwendung: Wordpress, Lighttpd, MySQL, PHP.

5Freitag, 13. Mai 2011

Page 6: bephpug - varnish & co

PerformanceschweinWordpress

stadt-bremerhaven.de

6Freitag, 13. Mai 2011

Page 7: bephpug - varnish & co

lawblog.de: 2005

„Wahnsinn, 3.000 Besucher an einem Tag!“

..und dann kam Spiegel Online

Load > 70, lokale Konsole tot

Apache 1.3, Wordpress 1.5, nichts optimiert

7Freitag, 13. Mai 2011

Page 8: bephpug - varnish & co

Schnapsidee: Blacklist

Viele Bots, Crawler, komische Scripte

wget -m http://www.lawblog.de/

„Offline lesen“ (...)

Idee: Automatisiert die Top-5-Traffic-erzeuger jeden Monat in die Blacklist.

8Freitag, 13. Mai 2011

Page 9: bephpug - varnish & co

Super Plan.

b3091322.crawl.yahoo.net ist übrigens inzwischen seit Monaten Traffic-Platz 1..

9Freitag, 13. Mai 2011

Page 10: bephpug - varnish & co

lawblog.de: 2010300.000 Hits pro Tag

busybox httpd/1.35 varnish 2.1 + lighttpd

Eigener Server bei vollmar.net

10Freitag, 13. Mai 2011

Page 11: bephpug - varnish & co

Aktuelles Setup

pound

varnish lighttpd php-fcgi

greensql*

mysql

wp_cache

XCache

monit

munin

11Freitag, 13. Mai 2011

Page 12: bephpug - varnish & co

varnish

Reverse Proxy

„Squid is 1975 architecture, varnish 2006“

http://varnish-cache.org/wiki/ArchitectNotes

„One varnish doing about 4320 million req/day, peaks at 8K reqs/s.

This particular varnishbox is a HP Proliant BL460c G1 2XDualcore 2.66 Ghz with 6 GB of RAM.Running Centos5.3. Only tuning is /var/lib/varnish on tmpfs“

(Aus: varnish-misc Mailingliste)

12Freitag, 13. Mai 2011

Page 13: bephpug - varnish & co

varnish: Features

VCL - Configlanguage

Loadbalancing with health checking of backends

Partial ESI support

URL rewriting

Graceful handling of dead backends

13Freitag, 13. Mai 2011

Page 14: bephpug - varnish & co

Varnish: Konzept

14Freitag, 13. Mai 2011

Page 15: bephpug - varnish & co

Varnish: VCL

sub vcl_fetch { if (req.url ~ "^/index.php/archives/200") { set beresp.http.Cache-Control = "max-age=1000"; set beresp.ttl = 600s; unset beresp.http.set-cookie; return (deliver); } if (req.url ~ "^/$") { set beresp.http.Cache-Control = "max-age=30"; set beresp.ttl = 15s; unset beresp.http.set-cookie; return (deliver); } if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") { set beresp.http.Cache-Control = "max-age=14400"; set beresp.ttl = 1w; unset beresp.http.set-cookie; return (deliver); }

15Freitag, 13. Mai 2011

Page 16: bephpug - varnish & co

Varnish: VCL

TTL und Co für die jeweilige Seite anpassen

Alter Content darf länger in den Cache

Grafiken: welche ändern sich oft, welche nie?

vcl_hash: „Was ist der selbe Request“

16Freitag, 13. Mai 2011

Page 17: bephpug - varnish & co

Varnish: VCL

In VCL programs, C program code can be put anywhere, simply by enclosing it in C{ ... }C

sub vcl_fetch { if (req.url ~ "^/something-special") { C{! ! syslog(LOG_ERR, "Spurious response from backend: xid %s request %s %s \"%s\" %d \"%s\" \"%s\"", VRT_r_req_xid(sp), VRT_r_req_request(sp), VRT_GetHdr(sp, HDR_REQ, "\005host:"), VRT_r_req_url(sp), VRT_r_obj_status(sp), VRT_r_obj_response(sp), VRT_GetHdr(sp, HDR_OBJ, "\011Location:"));! }C }

17Freitag, 13. Mai 2011

Page 18: bephpug - varnish & co

Varnish: ESISeitenteile getrennt cachen

z.B. „Widgets“

nicht alle ESI Featuresindex.html:<HTML><BODY>The time is: <esi:include src="/cgi-bin/date.cgi"/> at this very moment.</BODY></HTML>

config:sub vcl_fetch { if (req.url == "/index.html") { esi; /* Do ESI processing */ set obj.ttl = 24 h; } elseif (req.url == "/cgi-bin/date.cgi") { set obj.ttl = 1m; }}

18Freitag, 13. Mai 2011

Page 19: bephpug - varnish & co

Varnish: Load balancing

Health Polling

Kaputtes Backend = „auslassen“

Backendwahl: Random, Round-Robin

..oder via VCL

19Freitag, 13. Mai 2011

Page 20: bephpug - varnish & co

Varnish: Backend tricks

Neuen Content abholen während alter noch ausgeliefert wird

Defektes Backend: Interne TTL hochdrehen („grace“)

Gestaltbare Fehlermeldung

„Der Webserver wird einer Wartung unterzogen. Etwas Geduld.“

20Freitag, 13. Mai 2011

Page 21: bephpug - varnish & co

Varnish: Purge

Content explizit invalidieren

Über Telnet-Schnittstelle oder Console-Tool

Auch Wildcards

Optional: HTTP Purge Requests

Das Webscript kann selbst Updates triggern

21Freitag, 13. Mai 2011

Page 22: bephpug - varnish & co

Advanced Features

varnish-misc Mailingliste

Unterschiedliche Behandlung von Mobilclients

sticky load balancing

Mehrere Varnish-Instanzen

22Freitag, 13. Mai 2011

Page 23: bephpug - varnish & co

Varnish: Tut nicht

HTTPS

Cookies

„eigene“ Content-Modifikationen

Logfiles

23Freitag, 13. Mai 2011

Page 24: bephpug - varnish & co

Varnish: HTTPS

varnish spricht nur plain http

„Fix“: Pound oder Stunnel davor

Problem: $_SERVER[„HTTPS“];

Problem: $_SERVER[„REMOTE_ADDR“];

24Freitag, 13. Mai 2011

Page 25: bephpug - varnish & co

Varnish: Cookies

Philosophie: „nur nicht personalisierte Seiten cachen“

Workaround: unset beresp.http.set-cookie;

25Freitag, 13. Mai 2011

Page 26: bephpug - varnish & co

Varnish: Content Modifikationen

Varnish „interessiert“ Seiten-Content nie

ausser ESI

Nur Header-Modifikationen

in beide Richtungen

26Freitag, 13. Mai 2011

Page 27: bephpug - varnish & co

Varnish: logfiles

Varnish hat sehr gute Debuggingtools

aber: Log nur in shared memory, wird nicht geschrieben

„Fix“: varnishncsa

Liest shared memory, schreibt nach Apache-format-Log

27Freitag, 13. Mai 2011

Page 28: bephpug - varnish & co

varnish: 2.1

Bei Howtos aufpassen:

varnish 2.1 ist an verschiedenen Stellen mit anderer Syntax

pre-2.1-Config lassen sich nicht immer starten

machen seltsame Dinge

Änderung: req heisst nun beresp

28Freitag, 13. Mai 2011

Page 29: bephpug - varnish & co

Varnish: Tools

varnishhist

Geschwindigkeits-Histogramm

varnishstat

„top“ für varnish

varnishlog

zeigt/filtert shared memory log

29Freitag, 13. Mai 2011

Page 30: bephpug - varnish & co

varnish: Quick‘n dirty

Webserver von Port 80 wegziehen

vi /etc/default/varnish

varnish auf Port 80

vi /etc/varnish/default.vcl

backend anpassen: Webserver-IP und -Port

Starten, fertig.

30Freitag, 13. Mai 2011

Page 31: bephpug - varnish & co

Bonustrack: Monit

varnish war früher wacklig

Bei Wartungsdowntime nicht vergessen :-)

Monit checkt alle n Minuten die Prozesse

/etc/monit/conf.d/varnishcheck process varnish with pidfile /var/run/varnishd.pidstart program = "/etc/init.d/varnish start"stop program = "/etc/init.d/varnish stop"if totalmem > 1024.0 MB for 5 cycles then restartif loadavg(5min) greater than 10 for 8 cycles then stopif 3 restarts within 5 cycles then timeout

31Freitag, 13. Mai 2011

Page 32: bephpug - varnish & co

Bonustrack: MuninWiggly lines!

Rudimentäres Tresholdmonitoring, nagios-Anbindung

32Freitag, 13. Mai 2011

Page 33: bephpug - varnish & co

Wiggly linesWas bringt varnish eigentlich?

komplett aus

komplett annur / und feeds

33Freitag, 13. Mai 2011

Page 34: bephpug - varnish & co

Wiggly lines II

34Freitag, 13. Mai 2011

Page 35: bephpug - varnish & co

Genug varnish.

35

pound

varnish lighttpd php-fcgi

greensql*

mysql

wp_cache

XCache

monit

munin

Freitag, 13. Mai 2011

Page 36: bephpug - varnish & co

pound

Kann Loadbalancing

mit Session Handling

Kann bis auf caching viel was varnish auch kann

HTTP Sanitizing

Bei mir: Reiner HTTPS->HTTP Wrapper

36Freitag, 13. Mai 2011

Page 37: bephpug - varnish & co

wp_cache

Wordpressplugin

Schreibt gerenderte Seite nach HTML

Neugeneration via wp-event-handler

„Müsste man mal:“

wp_cache triggert Purge-Request bei varnish

37Freitag, 13. Mai 2011

Page 38: bephpug - varnish & co

wp_cache

Kann auch via .htaccess bzw lighty-lua direkt auf HTML, kein PHP-Interpreter mehr im Spiel

Nur noch historisch anwesender „Fallback“, bringt wegen varnish nur wenig Geschwindigkeit

38Freitag, 13. Mai 2011

Page 39: bephpug - varnish & co

XCache: Opcode

PHP Opcode Caching

Unbedingt Default-Config „aufbohren“

Web-Interface hilft

39Freitag, 13. Mai 2011

Page 40: bephpug - varnish & co

XCache: varcache

Kann auch varcache

„memcached für arme“

Weniger Features, aber dafür ein Daemon weniger

Obskure Race-Conditions bei Wordpress

40Freitag, 13. Mai 2011

Page 41: bephpug - varnish & co

MySQL „Tuning“

Halbe „Wissenschaft“

Gute Literatur

Aber: Oft reicht auch die faule Variante

..und ich bin gern faul :-)

http://www.day32.com/MySQL/

tuning_primer.sh

41Freitag, 13. Mai 2011

Page 42: bephpug - varnish & co

tuning_primer.sh

Braucht „bc“

MySQLd sollte mindestens 24 Stunden laufen

sh ./tuning_primer.sh

„Übersetzt“ MySQL-Status-Variablen

Read-Only

Nicht einfach blind übernehmen

42Freitag, 13. Mai 2011

Page 43: bephpug - varnish & co

tuning_primer.sh

43Freitag, 13. Mai 2011

Page 44: bephpug - varnish & co

GreenSQL

„MySQL-Firewall“

Hängt vor dem MySQLd

Versucht Injections zu erkennen

learning mode

„Risk score“

SELECT ... WHERE user=x AND password=y OR 1

44Freitag, 13. Mai 2011

Page 45: bephpug - varnish & co

GreenSQL

Snake Oil?

„dann brauchen wir ja im Code nicht mehr validieren“

Eher: Letzte Rettung bei Wordpress 0-days

Wenn der Proof of Concept Exploit nicht tut, geht das Scriptkidde vielleicht einfach weg

45Freitag, 13. Mai 2011

Page 46: bephpug - varnish & co

Caching

„Your mileage may vary“

Caching massiv Anwendungsspezifisch

Blind überall cachen bringt Ärger

Race Conditions

Redundante Datenhaltung

Cache purging?

46Freitag, 13. Mai 2011

Page 47: bephpug - varnish & co

Caching

Besser: Bottlenecks finden

PHP Profiling

Benchmarks

Ideal: Applikation braucht gar keinen Cache

47Freitag, 13. Mai 2011

Page 48: bephpug - varnish & co

Danke :-)

Fragen?

http://www.varnish-cache.org

http://www.apsis.ch/pound/

http://www.greensql.net

http://xcache.lighttpd.net/

48Freitag, 13. Mai 2011

Page 49: bephpug - varnish & co

Aktuelles Setup

pound

varnish lighttpd php-fcgi

greensql*

mysql

wp_cache

XCache

monit

munin

49Freitag, 13. Mai 2011

Page 50: bephpug - varnish & co

50Freitag, 13. Mai 2011