Devinsampa nginx-scripting

1,547 views
1,327 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,547
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
35
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Devinsampa nginx-scripting

  1. 1. NGINX SCRIPTINGEXTENDING NGINX FUNCTIONALITIES WITH LUA Tony Fabeen / @tonyfabeen / SlimStacks
  2. 2. WHO AM I
  3. 3. NGINX ("ENGINE-X")High performance HTTP, POP/IMAP and reverse proxy server.Started in 2002 by Igor Sysoev, public in 2004.Entirely written in C.Hosts nearly 12.18% of active sites across all domains.Nginx.com in 2011.
  4. 4. MASTER WORKER MODEL
  5. 5. $p ax|ge nix s u rp gnro 339..nix mse poes/p/gn/bnnix ot 12 . gn: atr rcs otnixsi/gnww 330..nix wre poes w 13 . gn: okr rcs
  6. 6. MASTER PROCESSreading and validating configurationcreating, binding and closing socketsstarting, terminating and maintaining the configured number of w r e okrprocessesre-opening log filescompiling embedded Perl scripts
  7. 7. WORKER PROCESSDo all important stuffHandle connection from clientsReverse Proxy and Filtering functionalities
  8. 8. REQUEST PROCESSING
  9. 9. REQUEST PHASES
  10. 10. SERVER REWRITE PHASErequest URI transformation on virtual server level
  11. 11. FIND CONFIG PHASEconfiguration location lookup
  12. 12. REWRITE PHASErequest URI transformation on location level
  13. 13. ACCESS PHASEaccess restrictions check phase
  14. 14. TRY FILES PHASEtry_files directive processing phase
  15. 15. CONTENT PHASEcontent generation phase
  16. 16. LOG PHASE logging phase
  17. 17. MODULARITYCore ModuleFunctional Modules
  18. 18. CORE MODULEEvent LoopModule execution control
  19. 19. FUNCTIONAL MODULESRead from / Write to Network and StorageTransform ContentOutbound FilteringServer Side IncludesUpstream Server communication...etc
  20. 20. LUA ON THE STAGE
  21. 21. A BIT OF LUA Created in Brazil Portable Simple Small Easy to embed Fast
  22. 22. OSS USING LUA
  23. 23. LUA NGINX MODULE https://github.com/chaoslawful/lua-nginx-module/Created by TaoBao.com EngineersHigh concurrent and non-blocking request processingPrograms can be written in the plain-old sequential wayNginx takes care of I/O operations and Lua Nginx Module restore thecontext and resume the program logic
  24. 24. LUA NGINX MODULE https://github.com/chaoslawful/lua-nginx-moduleIntroduces directives for running Lua inside NginxExposes the Nginx environment to Lua via an ApiIts fastIs even faster when compiled with LUA JIT(Just in Time Compiler)
  25. 25. NGINX LUA API
  26. 26. DIRECTIVESConfiguration directives serve as gateways to the Lua API within the nginx.conf file. cnetb_u LASRP_TIG otn_yla U_CITSRN rwieb_u LASRP_TIG ert_yla U_CITSRN acs_ylaLASRP_TIG cesb_u U_CITSRN cnetb_u_iePT_OLASRP_IE otn_ylafl AHT_U_CITFL rwieb_u_iePT_OLASRP_IE ert_ylafl AHT_U_CITFL acs_ylafl PT_OLASRP_IE cesb_u_ie AHT_U_CITFL Unless you set l a c d _ a h to o f modules will be loaded once on the first request. u_oecce f ,
  27. 27. NGX PACKAGENginx Environment is exposed via n x g package nxagulag g.r.r_r nxvrVRAL_AE g.a.AIBENM nxhae.EDRATIUE g.edrHAE_TRBT nxcx g.t
  28. 28. HELLO WORLD ! lcto /el-srb-u { oain hloue-yla dfuttp "etpan; eal_ye tx/li" cnetb_u otn_yla nxsy"el," nxvragnm,"" g.a(Hlo , g.a.r_ae !) ; } lcto /el-srb-gn { oain hloue-ynix eh "el,$r_ae!; co Hlo agnm " }$cr ht:/oahs/el-srb-u?aeDvnap ul tp/lclothloue-ylanm=eISma Hlo Dvnap ! el, eISma$cr ht:/oahs/el-srb-gn?aeDvnap ul tp/lclothloue-ynixnm=eISma Hlo Dvnap ! el, eISma
  29. 29. NGINX VARS lcto /csignixag { oain aesn-gn-rs st$is 3; e frt 5 st$eod6; e scn 5 stb_u $u e_yla sm rtr nxvrfrt+nxvrscn eun g.a.is g.a.eod ; eh "h smi $u" co Te u s sm; }$cr ht:/oahs/csignixag ul tp/lclotaesn-gn-rs Tesmi 9 h u s 9
  30. 30. NGINX SUBREQUESTSlcto /u-urqet {oain lasbeuss cnetb_u otn_yla lclrsos =nxlcto.atr(/el-srb-gn?ae oa epne g.oaincpue"hloue-ynixnm=Dvnap"eISma) i rsos.tts> 50te f epnesau = 0 hn nxei(epnesau) g.xtrsos.tts ed n nxsau =rsos.tts g.tts epnesau nxsyrsos.oy g.a(epnebd) ;}$cr ht:/oahs/u-urqet ul tp/lclotlasbeuss Hlo Dvnap ! el, eISma
  31. 31. NON BLOCKING I/O SUBREQUESTSlcto /nltc-nrmn { oain aayisiceet cnetb_u otn_yla lclrsos =nxlcto.atr(/ei" oa epne g.oaincpue"rds, {rs={m ="nr,ky=nxvragln}) ag cd ic" e g.a.r_ik} nxsy"nrmne t :,nxvragln) g.a(Iceetd o " g.a.r_ik ;}lcto /ei { oain rds itra; nenl stuecp_r $e $r_e; e_nsaeui ky agky stuecp_r $m $r_m; e_nsaeui cd agcd rds_ur $m $e; ei2qey cd ky rds_as1700167; ei2ps 2...:39}$cr ht:/oahs/nltc-nrmn?ikht:/w.eismacmb ul tp/lclotaayisiceetln=tp/wwdvnap.o.r Iceetdt :tp/wwdvnap.o.r nrmne o ht:/w.eismacmb
  32. 32. FILTERS
  33. 33. HEADER FILTERSlcto /{ oain poyps ht:/oahs:00 rx_as tp/lclot88; hae_itrb_u g.edrSre ="yLtl Sre"; edrfle_yla nxhae.evr M ite evr}$cr - - HA ht:/oahs/edrfle ul i X ED tp/lclothae-itr HT/. 20O TP11 0 K Dt:Sn 0 Sp21 2:81 GT ae u, 9 e 02 11:1 M Sre:M Ltl Sre evr y ite evr CnetTp:tx/tlcastuf8 otn-ye ethm;hre=t- CnetLnt:49 otn-egh 4 Cneto:ke-lv oncin epaie Sau:20O tts 0 K XFaeOtos smoii -rm-pin: aergn XXSPoeto:1 md=lc -S-rtcin ; oebok XCsae ps -acd: as XRc-ah:ms -akCce is
  34. 34. BODY FILTERSlcto /oyfle {oain bd-itr eh "ycnet; co M otn" bd_itrb_u oyfle_yla nxag1 =srn.sbnxag1,"y,"or) g.r[] tiggu(g.r[] M" Yu" nxag2 =tu -stefo ls canbfe g.r[] re -e o r at hi ufr ;}$cr ht:/oahs/oyfle ul tp/lclotbd-itr Yu cnet or otn
  35. 35. COSOCKETSNon Blocking, of courseCommunicate via TCP or Unix domain socketsKeepalive mechanism avoid connect/close for each request
  36. 36. COSOCKETSlcto /ecce-rmla{oain mmahdfo-u cnetb_u otn_yla lclsc =nxsce.onc(17001,121 oa ok g.oktcnet"2..." 11) lclbts er=sc:ed"e fobrrn) oa ye, r oksn(st o a" i ntbtste f o ye hn nxsy"aldt sn. " er g.a(fie o ed. n, r) rtr eun ed n lcldt =sc:eev( oa aa okrcie) i ntdt te f o aa hn nxsy"aldt rciedt." g.a(Fie o eev aa.n) ed n nxsy"eut:" dt) g.a(Rsl , aa ;}
  37. 37. SOME LIBRARIES USING PURE COSOCKETS https://github.com/agentzh/lua-resty-memcached https://github.com/agentzh/lua-resty-redis https://github.com/agentzh/lua-resty-mysql
  38. 38. SUMMARYThe Nginx architecture is excellent for highly scalable applications.Nginx can do a variety of things thanks to module extensions, and one canresuse those extensions by issuing sub-requests in Lua.lua-nginx-module makes use of the evented architecture in Nginx,providing a powerful and performant programming environment.Its possible to do 100% non-blocking I/O with readable code.
  39. 39. REFERENCEShttp://www.aosabook.org/en/nginx.htmlhttp://openresty.orghttp://www.evanmiller.org/nginx-modules-guide.htmlhttp://wiki.nginx.org/HttpLuaModule
  40. 40. BOOKS
  41. 41. QUESTIONS ?
  42. 42. THANKShttp://www.twitter.com/tonyfabeenhttp://www.linkedin.com/in/tonyfabeenhttps://github.com/tonyfabeen

×