Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

4Developers 2015: Varnish tips & tricks - Piotr Pasich

265 views

Published on

Piotr Pasish

Language: Polish

Phil Karlton twierdzi, że są tylko dwie trudne rzeczy w programowaniu - nazewnictwo oraz inwalidacja cache. Można śmiało powiedzieć, że nie tylko inwalidacja, ale cache sam w sobie. Jak prawie wszystko w programowaniu, tak i Varnish został stworzony, by rozwiązywać najczęstsze problemy - tym razem związane z chwilowym przechowywaniem danych oraz dostarczaniem ich użytkownikowi w jak najkrótszym czasie.

Prezentacja obejmuje przedstawienie najpopularniejszych problemów oraz metod ich rozwiązywania z Varnish oraz przegląd zaawansowanych możliwości narzędzia - zarządzanie cachem, praca z API, security, a nawet streamingiem danych przez http.

Published in: Software
  • Be the first to comment

  • Be the first to like this

4Developers 2015: Varnish tips & tricks - Piotr Pasich

  1. 1. VARNISH TIPS & TRICKS
  2. 2. Ant, composer and code quality tools HI - WW. .. , ~ 1 . , ms». .. I9 % .0 ‘ T Piotr Pasich rvnr» <, II-. lI rl>‘IE'll I I. I -. y Follow @pIoIIpasIcn ZZZ CATEGORIES I-I'IIII. It I 919 I I: I*. IIII, I»'I 5 IIe— . <]l -. -I. ' . -‘.40-‘l.1Ilr'i. -. III e-.1/H . — lllt1lI. vI III -I rll lllI‘ll[v -I)I'.1l‘nII‘I‘r-lj‘Il>-I-—0IN Ir. ..’ 4.II. ‘4.4I. _.. ‘.. aIIit. IYIII‘*: .1Dfi"IJfVIjZ‘,7 “. 'I‘: I'Il I-"~, ’fIt:1“‘-‘CHI rs,
  3. 3. Ant, composer and code quality tools Piotr Pasich ‘ ‘ jg ' i' : II: :'I M. - H, p. . I. _-, . . l‘ , Size Time . . ‘ Timeline I : Content Latency m - 10.6 KB 370 ms if‘‘ifffi. , 38.8 KB
  4. 4. Ant, composer and code quality tools N _ . _ . . . ., I., ..I . -~ I ' » [§]sI. ... 15- L“ . , , 41%. . 1 I | T _ III I ' Size Time I’ Fo| |oW@DicIlI| :Iasi(l I : Content Latency E » 10.6 KB 70 ms 38.8 KB Piotr Pasich EI‘I: ;'I », I . H‘ p. . , ._-, , I‘I'r-I l CATEGORIES . -I : /’Ki_jYT ». ... I.-. I. » I’/ ‘5‘I. — III. .III, ;VlI II, .;IIIIII_I-. 1 '. I‘. I)r? l‘LI . ,r. ~ I. I>xII IIIl. ’IlI'I-l, I. I I‘! -H‘ I»-.1.’ I‘ —I. II HI I ', “.Il *-I’-I >1-. ~ I-I . I;. ,I». ,. 4,II’. r(l : uL~— , II. .I-‘ I: : . I, :|llI I I «, .I: »_III em, ,-. ;:I, I ,4 II 2:-«I II . ‘.IV'I‘I »-I. I.-, IIIu II‘, I: I«—s<--, :I, I.II 'I
  5. 5. A VARNISH CACHE
  6. 6. , ., C-~"‘. >3‘$‘-35‘ O0 -‘, ..‘9gI‘ .4IoI - “sat ‘IJ
  7. 7. n‘: '9» (3% to I ‘> I. . r‘ I» I l%}a: *
  8. 8. . O VARNISH *:80
  9. 9. ..0 VARNISH *:80
  10. 10. Installation $ apt-get update $ apt-get upgrade $ apt-get install varnish
  11. 11. /etc/ default/ varnish DAEMON_OPTS= "-a 26081 -T | oca| host:6082 -f / etc/ varnish/ defau| t.vc| -S / etc/ varnish/ secret -s ma| |oc,256m"
  12. 12. /etc/ default/ varnish DAEMON_OPTS= "-a :80 -T | oca| host:8061 -f / etc/ varnish/ defau| t.vc| -S / etc/ varnish/ secret -s ma| |oc,256m"
  13. 13. czzzzztazfi View stats Conficure )a1‘amete1‘s Edit VCL Node manacement Mana ement. conso Statistics collected at Thu Feb 19 09:10:55 2009 Summary Q public-web (G) Q private-1 (N) show to Connect requests . . "“’ ’ ’ ""‘ ”"““‘ g 5.30 g 5.66 0 0 09:10:00 09:10:13 09:10:27 09:10:41 : 00:10:00 09:10:13 09:10:27 09:10:41 09:1 Time Time Last: M111 2 E Last: Mi H D E since cechc hit ratio since start Cache hit ratio since start
  14. 14. /etc/ varnish/ defau| t.vc| backend default { . host = "127.0.0.1"; .port = "8080"; }
  15. 15. apache NameVirtua| Host *:8080 Listen 8080
  16. 16. /etc/ varnish/ defau| t.vc| sub vc| _fetch { set beresp. tt| = 5m; }
  17. 17. Administration panel sub vc| _recv { if (req. http. host ~ "piotrpasich. com" | | req. ur| ~ ""/ wp-admin" | | req. http. Cookie ~ "user") { return (pass); } unset req. http. Cookie; }
  18. 18. I vc| _fetch? vc| _recv?
  19. 19. @ vc| _deIiver
  20. 20. Administration panel sub vc| _recv { if (req. http. host ~ "piotrpasich. com" | | req. ur| ~ ""/ wp-admin" | | req. http. Cookie ~ "user") { return (pass); } unset req. http. Cookie; }
  21. 21. return (pass)
  22. 22. pass pipe hash purge
  23. 23. @ vc| _deIiver
  24. 24. ’ 70° Cuperlino Mostly Sunny . 7‘, Reply Dismiss
  25. 25. Headers Cache-Control (max-age, s-max-age) Expues ETag Last-Modified
  26. 26. Good practices
  27. 27. Good practices Let the backend dictate the rules
  28. 28. Good practices Let the backend dictate the rules Do not overwrite
  29. 29. PURGE vs BAN
  30. 30. method = PURGE
  31. 31. BAN ban req. http. host = = "examp| e.com" && req. ur| ~ ". png$" ban(req. http. hos = = "examp| e.com" && req. ur| ~ ". png$")
  32. 32. BE! 0 Wm NEWS Nome Vita: Warm up As « vuopered Med dsasler A new EU approacw II A vast sum» :71 sea S Africa violence my rar 2:: name me see» w am; r . r we wave of 3;. » wmgraris we '0'“: arms *1 — er says O 2 rovssgo 3 Hundreds pm oezae much Alnsani calllm SA buycan Find an FT subscription package to suit you » More than 3(1) held over Poland News Spent Weather shop Earth Travel Mo1e~ sent: (:1 ausmess rm see-we Magazne En'ev1alVnai‘KAr15 wean ADVERTIBAENT Turin Shroudooes back on dsnlay O ; m-sap : :;'I“°"°"°": "'“‘ F W’ O EP: >.. vs3;: '- ¢: <r ' "~ A ' ' nun Plncomiqnns MY '3‘ Fm Is releases new -, m,. .., ,". ,¢¢ Holocaust comment 'kili| g'vi1eo > zi (_~, :,, _,. ;., = :ar: - 'x~: ~"s me .25 ' '33:’ 3 W ‘"35 5 1' Hundredsioin SAfrica ; , V 2r -2 43196 §iYE<DV JEMES xaz nnrd‘ Gamay 0 e r3wss; ¢ Flnnish m Stubh F M. ,5 ‘,1 V concedes dekal ‘I? -‘_, ,'W V‘ cm“ Obama sm Mes Poles _ orrwsago Odlmruisaga h angaru Europe’: Silbon Vdley? O 43 'w'i. E§ lg: Emu kiled at am: floolaal dub Hani on win samin mm Prix mu“ ,5 0" mm. ’ 0 ' "= u'2;= » " I W‘ Iougnoniaoe O 4: "Hues 3,70 an Airlock ‘waned nlaveunoenlor % N Koteai Ieaderunales nnuntair himelf in usage -¢: '“r: O - 4:». Markets
  33. 33. <! DOCTYPE htm| > <html> <body> <! -- some content --> <esi: include src= "http: //. .." / > <! -- more content --> </ body> </ html>
  34. 34. Saint mode
  35. 35. sub vcl_fetch { if (beresp. status = = 500) { set beresp. saintmode = 203; if (req. request ! = "POST") { return(restart); }eBe{ _ error 500 "Fai| ed"; } } set beresp. grace = 1 h; }
  36. 36. Grace mode
  37. 37. sub vcl_recv { ii" (rec§. backend. healthy) { set req. grace = 30s; }eBe{ H set req. grace = ‘in; } } sub vcl_fetch { set beresp. grace = ‘i h; }
  38. 38. Load balancing
  39. 39. backend foo { . host = "foo. example. com"; } backend bar { . host = "bar. example. com"; }
  40. 40. director baz round-robin { { . backend = foo; . backend = bar; } { }
  41. 41. sub vcl_recv { if (req. http. host ~ ""(www. )?mysite. com$") { set req. backend = baz; } }
  42. 42. VMOS
  43. 43. _ VMODS behaviors can be changed https: //www. varnish-cache. org/ vmods
  44. 44. Debug
  45. 45. sub vcl_deIiver{ 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; }eBe{ G set resp. http. X-Cache = "iVilSS"; } deliver; }
  46. 46. X-Varnish
  47. 47. 4 VARNISH ‘ CACHE When use? When don't?
  48. 48. 4 VARNISH ‘ CACHE 4 0 Cookies Sessions SSL Content type ‘min 1 API
  49. 49. 4 VARNISH ‘ CACHE Assets Dynamic assets API ?
  50. 50. . 1 . Thank you . O D @piotrpasich www. piotrpasich. com ’ X-TEAM" II-I. H.u. . 1 "

×