Upgrade to Pro — share decks privately, control downloads, hide ads and more …

mod_perl温故知新 〜Perl CGIの高速化からメールサーバまで〜

mod_perl温故知新 〜Perl CGIの高速化からメールサーバまで〜

2011/07/16に Hokkaido.pm#5 で発表したスライドです。

OGATA Tetsuji

July 16, 2011
Tweet

More Decks by OGATA Tetsuji

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ(1/2) • ඌܗ మ࣍ (OGATA Tetsuji) Twitter: @xtetsuji • http://post.tetsuji.jp/

    (࠷ۙϒϩά࢝Ί·ͨ͠) ͜͜ͰޠΓ͖Εͳ͍͜ͱ΋ॻ͖͍ͨͰ͢ • ๺ւಓͷԻߋொ(ଳ޿ࢢͷྡொ)ग़਎Ͱ͢ɻ ߴߍଔۀ·Ͱͷ18೥ؒΛ๺ւಓͰա͝͠· ͨ͠
  2. ͍·͞Β Apache+mod_perl? • ʲ࣭໰ʳWAFɺPSGI/PlackΛ࢖͑͹ɺ ApacheͳͲҙࣝ͠ͳ͍͍ͯ͘ͷͰ͸ʁ • ʲճ౴ʳ͔֬ʹͦ͏͔΋͠Ε·ͤΜɻ ͚ͩͲɺ஌ࣝͱͯ͠஌͓͍ͬͯͯ΋ଛ͸ ແ͍͸ͣͰ͢ •

    ྫ͑͹Plack+mod_perl2Ͱ΋ɺมͳόά Λ౿ΜͰ͠·͏͜ͱ΋͋ΔΑ͏Ͱ͢ ※ http://subtech.g.hatena.ne.jp/cho45/20101221/1292941055 ※1. WAF=Web Application Framework ※1
  3. ͍·͞Β Apache+mod_perl? • ࠓ΋Apache͸࠷΋ ࢖ΘΕ͍ͯΔαʔό • ApacheͷγΣΞ͸ 2011೥7݄ݱࡏ65.86% (2011೥6݄ͩͱ64.88%) (http://news.netcraft.com/

    archives/2011/07/08/ july-2011-web-server- survey.html ΑΓ) • ͍ͭͷੈ΋൒਺Ҏ্Apache Market Share for Top Servers Across All Domains August 1995 - June 2011
  4. mod_perlͷόʔδϣϯ • Apache1.3 → mod_perl1 • Apache2.x → mod_perl2 •

    1ͱ2Ͱ͸mod_perlͷจ๏ͷҧ͍͸େ͖͍ • ྺ࢙ͷڱؒͷmod_perl1.99(ཁ஫ҙ) • ※ݱࡏApache1͸deprecated͕ͩɺੈؒͰ͸ݱ໾ͱݴ͑ΔͷͰɺmod_perl1 ʹ͍ͭͯ΋ɺͳΔ΂͘৮Ε·͢
  5. mod_perlͷྺ࢙ • mod_perl1 fi rst public release: 1996/3/25 (http://perl.apache.org/dist/mod_perl-1.0-current/Changes ΑΓ)

    • mod_perl2 fi rst public release: 2002/4/6 (http://perl.apache.org/dist/mod_perl-2.0-current/Changes ΑΓ)
  6. • mod_perl1 • mod_perl2 • ͜͏ॻ͘͜ͱͰɺ֦ுࢠ”.pl”ͷPerl CGI ͕ߴ଎Խ͢Δ Perl CGI

    ͷߴ଎Խ AddHandler perl-script .pl PerlHandler Apache::Registry AddHandler perl-script .pl PerlResponseHandler \ ModPerl::Registry # ↓Apache Con fi gurations
  7. Apacheͷ಺෦ॲཧ • ϦΫΤετΛड͚Δ • ϔομΛղੳ͢Δ • ඞཁʹԠͯ͡URLΛม׵ͨ͠ΓɺDocumentRootΛख͕͔Γʹ࣮ύε ΛׂΓग़͢ • ΞΫηε੍ޚɺೝূɺঝೝ(BASICೝূͳͲ)

    • MIMEλΠϓΛߟ͑Δ • ϨεϙϯεΛग़͢(੩తϑΝΠϧɺϓϩάϥϜग़ྗ) • ϩά(ΞΫηεϩάɺΤϥʔϩά)Λग़ྗ͢Δ • ࣍ͷϦΫΤετΛ଴ͭ
  8. ॲཧϑΣʔζ:mod_perl1 PerlChildInitHandler PerlPostReadRequestHandler PerlInitHandler PerlTransHandler PerlHeaderParserHandler PerlAccessHandler PerlAuthenHandler PerlAuthzHandler PerlTypeHandler

    PerlFixupHandler PerlFixupHandler PerlHandler PerlLogHandler PerlCleanupHandler PerlChildExitHandler ※Ұ෦লུ͕͋Γ·͢ɻৄ͘͠͸ҎԼΛࢀর http://perl.apache.org/docs/1.0/guide/con fi g.html#toc_Perl_Handlers
  9. ॲཧϑΣʔζ:mod_perl2 PerlChildInitHandler PerlPostReadRequestHandler PerlInitHandler PerlTransHandler PerlMapToStorageHandler PerlHeaderParserHandler PerlAccessHandler PerlAuthenHandler PerlAuthzHandler

    PerlTypeHandler PerlFixupHandler PerlFixupHandler PerlResponseHandler PerlLogHandler PerlCleanupHandler PerlChildExitHandler ※͔ͳΓলུ͕͋Γ·͢ɻ͍͔ͭ͘͸ޙ΄Ͳɻ·ͨ͸ҎԼΛࢀর http://perl.apache.org/docs/2.0/user/con fi g/con fi g.html
  10. ࣮ྫ: mod_rewriteͷ ϦϑΝΫλϦϯά • mod_rewriteॲཧΛɺURLΛॻ͖׵͑Δ ϑΣʔζͷPerlTransHandlerͰ࣮૷͢Δ • (࢖͏ͳΒ)৚݅෼ذ΍൓෮ॲཧ͕Perlͷ ίʔυʹͳΔͷͰՄಡੑ͕Α͘ͳΔ •

    mod_rewriteࣗ਎PerlTransHandlerʹରԠ ͢ΔC APIͷϑΣʔζͰಈ࡞͍ͯ͠·͢ ※࣮ࡍ͸PerlFixupHandlerʹରԠ͢ΔϑΣʔζͰ΋͓࢓ࣄΛ͍ͯ͠·͢
  11. • ʮϨεϙϯεϋϯυϥʯͱಉ͡Α͏ͳ จࣈ௨ΓʮϋϯυϥʯΛॻ͖·͢ • sub handler { my $r =

    shift; ... } • ଞͷϑΣʔζͰ΋͜ͷॻ͖ग़͠͸ఆੴ ࣮ྫ: mod_rewriteͷ ϦϑΝΫλϦϯά
  12. ଞʹͰ͖Δ͜ͱҰྫ • PerlPostReadRequestHandler औΓٸ͗ѱ࣭ͳDDoSΛःஅ(Ԡٸાஔ) • return DONE; # cut off!

    • ຊདྷ͸PerlAccessHandlerͰ͢Δͷ͕ྑ͍ • ࠷ऴతʹ͸LB΍iptablesͰःஅ͔ͳ
  13. ଞʹͰ͖Δ͜ͱҰྫ •طʹେྔͷHTMLϑΝΠϧͷ͋Δ੩తαΠτΛड ͚औͬͨϓϩάϥϚͷݽ܉ฃಆ • ґཔʮUser-AgentΛݟͯ *.html ͷ Content-Type Λ text/html

    ͔ application/xhtml+xml ͔ɺग़͠෼͚ͯ͠ ͍͚ͨͩ·͔͢ʁʯ • PerlTypeHandler ͔ PerlFixupHandler Ͱ΍Γ·͠ΐ͏ (ೖग़ྗϔομॲཧ: $r->headers_in, $r->headers_out)
  14. ଞʹͰ͖Δ͜ͱҰྫ • HTTPϨεϙϯεग़ྗॲཧ / PerlHandler / PerlResponseHandler ͕׬ྃͨ͠ޙ... • PerlLogHandler͸$r͔ΒHTTPϨεϙϯε

    ݁Ռ΍ૹ৴αΠζ਺͕औಘՄೳͳঢ়ଶ • ༷ʑͳDBʹϩάΛॻ͖ग़ͨ͠Γ௨஌Λ ͨ͠ΓͰ͖·͢
  15. Apache2&mod_perl2Ͱ HTTPͷ֎΁ • ઌ΄Ͳ঺հͨ͠mod_perlͷॲཧϑΣʔ ζ͸HTTP Request/Responseͷ͓࿩ • $r : Apache

    (mod_perl1) • $r : Apache2::RequestRec (mod_perl2) • ׳श໊ $r ͷ r ͸ request/response ͷ r
  16. Apache2/mod_perl2 Ͱͷ৽ઃαΠΫϧ • Apache2ʹ͸HTTPΛѻ͏Ҏ֎ͷαΠΫϧ΋͋Δ • ServerLifeCycle ؔ࿈ • Protocol /

    Connection ؔ࿈ • Filter ؔ࿈ • Thread ؔ࿈ •౰વmod_perl2Ͱ΋ɺͦΕΛ࢖ͬͯHTTPҎ֎ͷॲ ཧ͕ॻ͚Δ ※ http://perl.apache.org/docs/2.0/user/con fi g/con fi g.html Λࢀর
  17. Apache2 SMTPαʔόͷ ࡞੒ಈػ • ։ൃ಺༰͸ϝʔϦϯάϦεταʔϏε • Post fi x pipeΛ࢖͑͹PerlϓϩάϥϜͰI/O

    ॲཧ͕ॻ͚Δ͕ɺforkͷίετ͕ߴ͍ • ॏ͍DBॲཧɺֆจࣈॲཧ͕͔ͨͬͨ͠
  18. Apache2 SMTPαʔόͷߏ ੒ • ϋʔυ΢ΣΞߏ੒: 2୆(load balancing) • ֎෦ͱͷૹड৴SMTPαʔό͸ɺطʹ Քಇதͷผͷϋʔυ΢ΣΞ਺े୆ఔ

    ౓ͷΫϥελ͕͋Δ • ʮApache2 SMTPαʔόʯͷ಺෦Ͱ͸ɺ Post fi xʹड৴ͱૹ৴ͷॲཧΛґཔ...
  19. PerlProcessConnectionHandler Ͱॻ͍ͨSMTPαʔόͷৄࡉ Listen 10025 gm fi lter AcceptFilter gm fi

    lter none <VirtualHost 127.0.0.1:10025> TransferLog /var/log/httpd/gm fi lter.log ErrorLog /var/log/httpd/gm fi lter_error.log PerlModule Encode::NV \ ARM::G4::Apache2::GMFilter \ Apache::DBI PerlProcessConnectionHandler \ ARM::G4::Apache2::GMFilter </VirtualHost>
  20. qpsmtpd͸࣮͸ݎ͍ • qpsmtpdͷࠜఈΛࢧ͑ΔEngine=Transport • pipe (CGI like) • fork-server •

    prefork-server •Apache (Apache::Qpsmtpd) • async (Danga::Socket base?)
  21. ࢀߟจݙ • ༸ॻʹͳΓ·͕͢ɺmod_perlΛษڧ͢Δ্Ͱ ໾ཱͭ3࡭ • Practical mod_perl (Orelly 2003; http://modperlbook.org/)

    • The mod_perl Developer’s Cookbook (Sams Publishing 2002; http://www.modperlcookbook.org/) • mod_perl2 User’s Guide (Onyx Neon 2007; http://modperl2book.org/)
  22. ࢀߟจݙ • Apache C API(mod_*.c)ͷࢀߟॻ੶ • Apache֦ுΨΠυ(্ɾԼ) (Oreilly 2000; C

    APIΛmod_perlʹԊͬͯղઆͨ͠ྑॻͰ͕ͨ͠ ݱࡏઈ൛ͩͦ͏Ͱ͢) • The Apache Modules Book (Prentice Hall 2007; ͪ͜Β͸׬શʹC APIͷຊ)
  23. ࢀߟจݙ • WEB+DB PRESS Vol.33 (2006/6)ͷ࿈ࡌ ʮRecent Perl WorldʯͰҏ౻௚໵͞Μ͕ ʮʲୈ2ճʳmod_perl

    2.0Λ࢖͍౗͢ʯ ͱ͍͏هࣄΛॻ͍͍ͯ·͢ • ࣮ࡍʹNet::DNSΛ࢖ͬͯDNSαʔόΛ ॻ͍͍ͯΔσϞ΋ࢀߟʹͳΓ·͢ http://gihyo.jp/magazine/wdpress/archive/2006/vol33