SlideShare uma empresa Scribd logo
1 de 45
Baixar para ler offline
NGINX SCRIPTING
EXTENDING NGINX FUNCTIONALITIES WITH LUA
          Tony Fabeen / @tonyfabeen / SlimStacks
WHO AM I
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.
MASTER WORKER MODEL
$p ax|ge nix
        s u rp gn

ro 339..nix mse poes/p/gn/bnnix
 ot 12 . gn: atr rcs otnixsi/gn

ww 330..nix wre poes
 w 13 . gn: okr rcs
MASTER PROCESS
reading and validating configuration
creating, binding and closing sockets
starting, terminating and maintaining the configured number of w r e
                                                                okr
processes
re-opening log files
compiling embedded Perl scripts
WORKER PROCESS
Do all important stuff
Handle connection from clients
Reverse Proxy and Filtering functionalities
REQUEST PROCESSING
REQUEST PHASES
SERVER REWRITE PHASE
request URI transformation on virtual server level
FIND CONFIG PHASE
configuration location lookup
REWRITE PHASE
request URI transformation on location level
ACCESS PHASE
access restrictions check phase
TRY FILES PHASE
try_files directive processing phase
CONTENT PHASE
content generation phase
LOG PHASE
 logging phase
MODULARITY
Core Module
Functional Modules
CORE MODULE
Event Loop
Module execution control
FUNCTIONAL MODULES
Read from / Write to Network and Storage
Transform Content
Outbound Filtering
Server Side Includes
Upstream Server communication
...etc
LUA ON THE STAGE
A BIT OF LUA
 Created in Brazil
 Portable
 Simple
 Small
 Easy to embed
 Fast
OSS USING LUA
LUA NGINX MODULE
           https://github.com/chaoslawful/lua-nginx-module/
Created by TaoBao.com Engineers
High concurrent and non-blocking request processing
Programs can be written in the plain-old sequential way
Nginx takes care of I/O operations and Lua Nginx Module restore the
context and resume the program logic
LUA NGINX MODULE
         https://github.com/chaoslawful/lua-nginx-module
Introduces directives for running Lua inside Nginx
Exposes the Nginx environment to Lua via an Api
It's fast
Is even faster when compiled with LUA JIT(Just in Time Compiler)
NGINX LUA API
DIRECTIVES
Configuration 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 ,
NGX PACKAGE
Nginx 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
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
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
NGINX SUBREQUESTS
lcto /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
NON BLOCKING I/O SUBREQUESTS
lcto /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
FILTERS
HEADER FILTERS
lcto /{
 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
BODY FILTERS
lcto /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
COSOCKETS
Non Blocking, of course
Communicate via TCP or Unix domain sockets
Keepalive mechanism avoid connect/close for each request
COSOCKETS
lcto /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
    ';

}
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
SUMMARY
The Nginx architecture is excellent for highly scalable applications.
Nginx can do a variety of things thanks to module extensions, and one can
resuse 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.
It's possible to do 100% non-blocking I/O with readable code.
REFERENCES
http://www.aosabook.org/en/nginx.html
http://openresty.org
http://www.evanmiller.org/nginx-modules-guide.html
http://wiki.nginx.org/HttpLuaModule
BOOKS
QUESTIONS

   ?
THANKS
http://www.twitter.com/tonyfabeen

http://www.linkedin.com/in/tonyfabeen

https://github.com/tonyfabeen
Devinsampa nginx-scripting

Mais conteúdo relacionado

Mais procurados

Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with Perl
Kazuho Oku
 
Gitosis on Mac OS X Server
Gitosis on Mac OS X ServerGitosis on Mac OS X Server
Gitosis on Mac OS X Server
Yasuhiro Asaka
 
node.js workshop- node.js basics
node.js workshop- node.js basicsnode.js workshop- node.js basics
node.js workshop- node.js basics
Qiong Wu
 
node.js workshop- node.js middleware
node.js workshop- node.js middlewarenode.js workshop- node.js middleware
node.js workshop- node.js middleware
Qiong Wu
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
Michael Peacock
 
Clone database using_active_duplicate
Clone database using_active_duplicateClone database using_active_duplicate
Clone database using_active_duplicate
Viaggio Italia
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
Przemek Jakubczyk
 

Mais procurados (20)

Unix Programming with Perl
Unix Programming with PerlUnix Programming with Perl
Unix Programming with Perl
 
Gitosis on Mac OS X Server
Gitosis on Mac OS X ServerGitosis on Mac OS X Server
Gitosis on Mac OS X Server
 
Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD Relayd: a load balancer for OpenBSD
Relayd: a load balancer for OpenBSD
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
node.js workshop- node.js basics
node.js workshop- node.js basicsnode.js workshop- node.js basics
node.js workshop- node.js basics
 
node.js workshop- node.js middleware
node.js workshop- node.js middlewarenode.js workshop- node.js middleware
node.js workshop- node.js middleware
 
agri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertoragri inventory - nouka data collector / yaoya data convertor
agri inventory - nouka data collector / yaoya data convertor
 
Ssh cookbook
Ssh cookbookSsh cookbook
Ssh cookbook
 
Refactoring to symfony components
Refactoring to symfony componentsRefactoring to symfony components
Refactoring to symfony components
 
nouka inventry manager
nouka inventry managernouka inventry manager
nouka inventry manager
 
OWASP Proxy
OWASP ProxyOWASP Proxy
OWASP Proxy
 
Scaling antispam solutions with Puppet
Scaling antispam solutions with PuppetScaling antispam solutions with Puppet
Scaling antispam solutions with Puppet
 
Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6Joy of Six - Discover the Joy of Perl 6
Joy of Six - Discover the Joy of Perl 6
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Clone database using_active_duplicate
Clone database using_active_duplicateClone database using_active_duplicate
Clone database using_active_duplicate
 
Put on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and NodePut on Your Asynchronous Hat and Node
Put on Your Asynchronous Hat and Node
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android app
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 

Destaque

Search Lucene
Search LuceneSearch Lucene
Search Lucene
Jeremy Coates
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
Tony Fabeen
 
Intelligent crawling and indexing using lucene
Intelligent crawling and indexing using luceneIntelligent crawling and indexing using lucene
Intelligent crawling and indexing using lucene
Swapnil & Patil
 
An introduction to inverted index
An introduction to inverted indexAn introduction to inverted index
An introduction to inverted index
weedge
 

Destaque (20)

Nginx lua
Nginx luaNginx lua
Nginx lua
 
Architecture and implementation of Apache Lucene
Architecture and implementation of Apache LuceneArchitecture and implementation of Apache Lucene
Architecture and implementation of Apache Lucene
 
Search Lucene
Search LuceneSearch Lucene
Search Lucene
 
Introduction To Apache Lucene
Introduction To Apache LuceneIntroduction To Apache Lucene
Introduction To Apache Lucene
 
Solr
SolrSolr
Solr
 
Munching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processingMunching & crunching - Lucene index post-processing
Munching & crunching - Lucene index post-processing
 
Index types
Index typesIndex types
Index types
 
Text Indexing / Inverted Indices
Text Indexing / Inverted IndicesText Indexing / Inverted Indices
Text Indexing / Inverted Indices
 
From Lucene to Elasticsearch, a short explanation of horizontal scalability
From Lucene to Elasticsearch, a short explanation of horizontal scalabilityFrom Lucene to Elasticsearch, a short explanation of horizontal scalability
From Lucene to Elasticsearch, a short explanation of horizontal scalability
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
Lucene
LuceneLucene
Lucene
 
Lucene and MySQL
Lucene and MySQLLucene and MySQL
Lucene and MySQL
 
Nginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with LuaNginx Scripting - Extending Nginx Functionalities with Lua
Nginx Scripting - Extending Nginx Functionalities with Lua
 
Lucandra
LucandraLucandra
Lucandra
 
Inverted index
Inverted indexInverted index
Inverted index
 
Intelligent crawling and indexing using lucene
Intelligent crawling and indexing using luceneIntelligent crawling and indexing using lucene
Intelligent crawling and indexing using lucene
 
An introduction to inverted index
An introduction to inverted indexAn introduction to inverted index
An introduction to inverted index
 
Apache Solr/Lucene Internals by Anatoliy Sokolenko
Apache Solr/Lucene Internals  by Anatoliy SokolenkoApache Solr/Lucene Internals  by Anatoliy Sokolenko
Apache Solr/Lucene Internals by Anatoliy Sokolenko
 
Berlin Buzzwords 2013 - How does lucene store your data?
Berlin Buzzwords 2013 - How does lucene store your data?Berlin Buzzwords 2013 - How does lucene store your data?
Berlin Buzzwords 2013 - How does lucene store your data?
 
Introduction to solr
Introduction to solrIntroduction to solr
Introduction to solr
 

Semelhante a Devinsampa nginx-scripting

Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...
Odoo
 
Marko Gargenta_Remixing android
Marko Gargenta_Remixing androidMarko Gargenta_Remixing android
Marko Gargenta_Remixing android
Droidcon Berlin
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to Ansible
Mattias Gees
 

Semelhante a Devinsampa nginx-scripting (20)

Noah Zoschke at Waza 2013: Heroku Secrets
Noah Zoschke at Waza 2013: Heroku SecretsNoah Zoschke at Waza 2013: Heroku Secrets
Noah Zoschke at Waza 2013: Heroku Secrets
 
Beginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at GoogleBeginner workshop to angularjs presentation at Google
Beginner workshop to angularjs presentation at Google
 
Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...Tips on how to improve the performance of your custom modules for high volume...
Tips on how to improve the performance of your custom modules for high volume...
 
Marko Gargenta_Remixing android
Marko Gargenta_Remixing androidMarko Gargenta_Remixing android
Marko Gargenta_Remixing android
 
linux-namespaces.pdf
linux-namespaces.pdflinux-namespaces.pdf
linux-namespaces.pdf
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
 
Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.Null Bachaav - May 07 Attack Monitoring workshop.
Null Bachaav - May 07 Attack Monitoring workshop.
 
Keep it simple web development stack
Keep it simple web development stackKeep it simple web development stack
Keep it simple web development stack
 
Introduction to Ansible
Introduction to AnsibleIntroduction to Ansible
Introduction to Ansible
 
An Introduction to CSS Preprocessors
An Introduction to CSS PreprocessorsAn Introduction to CSS Preprocessors
An Introduction to CSS Preprocessors
 
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHPphp[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
php[world] 2016 - You Don’t Need Node.js - Async Programming in PHP
 
Zend con 2016 - Asynchronous Prorgamming in PHP
Zend con 2016 - Asynchronous Prorgamming in PHPZend con 2016 - Asynchronous Prorgamming in PHP
Zend con 2016 - Asynchronous Prorgamming in PHP
 
Javascript fundamentals for php developers
Javascript fundamentals for php developersJavascript fundamentals for php developers
Javascript fundamentals for php developers
 
Awesome Traefik - Ingress Controller for Kubernetes - Swapnasagar Pradhan
Awesome Traefik - Ingress Controller for Kubernetes - Swapnasagar PradhanAwesome Traefik - Ingress Controller for Kubernetes - Swapnasagar Pradhan
Awesome Traefik - Ingress Controller for Kubernetes - Swapnasagar Pradhan
 
Attack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaAttack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and Kibana
 
CoreOS: Control Your Fleet
CoreOS: Control Your FleetCoreOS: Control Your Fleet
CoreOS: Control Your Fleet
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
High Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando PatroniHigh Availability PostgreSQL with Zalando Patroni
High Availability PostgreSQL with Zalando Patroni
 
Application Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.keyApplication Logging in the 21st century - 2014.key
Application Logging in the 21st century - 2014.key
 

Último

Último (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Devinsampa nginx-scripting

  • 1. NGINX SCRIPTING EXTENDING NGINX FUNCTIONALITIES WITH LUA Tony Fabeen / @tonyfabeen / SlimStacks
  • 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.
  • 5. $p ax|ge nix s u rp gn ro 339..nix mse poes/p/gn/bnnix ot 12 . gn: atr rcs otnixsi/gn ww 330..nix wre poes w 13 . gn: okr rcs
  • 6.
  • 7. MASTER PROCESS reading and validating configuration creating, binding and closing sockets starting, terminating and maintaining the configured number of w r e okr processes re-opening log files compiling embedded Perl scripts
  • 8. WORKER PROCESS Do all important stuff Handle connection from clients Reverse Proxy and Filtering functionalities
  • 10.
  • 12. SERVER REWRITE PHASE request URI transformation on virtual server level
  • 14. REWRITE PHASE request URI transformation on location level
  • 16. TRY FILES PHASE try_files directive processing phase
  • 20. CORE MODULE Event Loop Module execution control
  • 21. FUNCTIONAL MODULES Read from / Write to Network and Storage Transform Content Outbound Filtering Server Side Includes Upstream Server communication ...etc
  • 22. LUA ON THE STAGE
  • 23. A BIT OF LUA Created in Brazil Portable Simple Small Easy to embed Fast
  • 25. LUA NGINX MODULE https://github.com/chaoslawful/lua-nginx-module/ Created by TaoBao.com Engineers High concurrent and non-blocking request processing Programs can be written in the plain-old sequential way Nginx takes care of I/O operations and Lua Nginx Module restore the context and resume the program logic
  • 26. LUA NGINX MODULE https://github.com/chaoslawful/lua-nginx-module Introduces directives for running Lua inside Nginx Exposes the Nginx environment to Lua via an Api It's fast Is even faster when compiled with LUA JIT(Just in Time Compiler)
  • 28. DIRECTIVES Configuration 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 ,
  • 29. NGX PACKAGE Nginx 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
  • 30. 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
  • 31. 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
  • 32. NGINX SUBREQUESTS lcto /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
  • 33. NON BLOCKING I/O SUBREQUESTS lcto /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
  • 35. HEADER FILTERS lcto /{ 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
  • 36. BODY FILTERS lcto /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
  • 37. COSOCKETS Non Blocking, of course Communicate via TCP or Unix domain sockets Keepalive mechanism avoid connect/close for each request
  • 38. COSOCKETS lcto /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 '; }
  • 39. 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
  • 40. SUMMARY The Nginx architecture is excellent for highly scalable applications. Nginx can do a variety of things thanks to module extensions, and one can resuse 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. It's possible to do 100% non-blocking I/O with readable code.
  • 42. BOOKS