mod_perl2ã§apacheã¢ã¸ã¥ã¼ã«ãä½ãã
mod_perlã使ã£ã¦Apache APIã®ã³ã¢ãªé åã«ã¡ãã£ã¨ã ããã£ã¬ã³ã¸ãã¦ã¿ããã¨æãã¾ãã
ããããã«ããã®ãã¨ã§ãâ
はてなダイアリーより
å··ã§ã¯mod_perlã¯CGIããã°ã©ã ãé«éåããããã®æè¡ã ã¨ãã解説ãææã¿ãããããCGIããã°ã©ã ãé«éåãããã¨ããç¹ã¯mod_perlã®ãã¤ç¹å¾´ã®ä¸é¨ã«éãããå®éã«ã¯Apache APIãPerlããå©ç¨ãããã¨ã§ãApacheå é¨ã®å¦çãã§ã¼ãºã«ããã¯ããå¦çãå®ç¾ã§ãããã¨ããç¹ãæ¬è³ªã§ããã
ã¨ããããã§ãæ©éããã¯éå§ï¼
ä»åãããããã¨ã¯
- mod_proxyï¼ãããã¯mod_proxy_balancerï¼ã«ç¬èªã®æ¯ãåããã¸ãã¯ãå°å ¥ããã
- æ¯ãåããã¸ãã¯ã¯mod_perl2ã使ã£ã¦Apacheã®ãªã¯ã¨ã¹ãå¦çãã§ã¼ãºã«ç´°å·¥ããããã¨ã§å®ç¾ããã
ã¨ãããã¨ã«ãã¦ã¿ã¾ãã
ä½è«ã§ãããè²ã
ã¨ã°ã°ã£ã¦ã¿ãã¨ããã
mod_proxy_balancerに独自振り分けロジックを追加できる気がするã¨ãããã¾ã£ãããã®ã¾ãã¾ã®ãã¼ãã«ææ¦ããã¦ããããã°ãçºè¦ããã®ã§ããããã¡ãã¯ãmod_proxy_balancerã®lbmethodãCã§å®è£
ãããã¨ãããã¨ããããçµæ§é£ããããªã®ã§ããã£ããã¨è«¦ãã¦ã¿ã¾ããï¼ï¼å¼±ï¼
ãã¦ãã¦ãå®éã«å®è£ ããåã«ããã¡ãã£ã¨ã ãåææ¡ä»¶ãè¨å®ãããã¨æãã¾ãã
- ããã³ãã®WEBãµã¼ãã¯è¤æ°å°ã«åé·åããã¦ããããããã«ãã®æåã§reverse proxyçãªãµã¼ãããã¦ãããã¤ãæ¯ãåãå¦çãè¡ã
- æ¯ãåãã«ä½¿ããã¼ã¯ãCookieã«ç¼ãããã¦ãã¼ã¯ãªã¦ã¼ã¶ã³ã¼ããã¨ããããã使ã£ã¦ãå ¨ã¦ã®ã¦ã¼ã¶ã¯æ¯åå¿ ãåä¸ã®WEBãµã¼ãã«ãã©ã³ã¹ããããããã«ãã¾ã
- ããã«ããã¯ã¨ã³ãã«ã¯è¤æ°ã®workerããã»ã¹ãå¾ ã¡æ§ãã¦ãã¦ãããã³ãã§åãä»ããã¦ã¼ã¶ã®ãªã¯ã¨ã¹ãæ å ±ãããããå¦çãããã
ã¨ãã¾ããããªæãã§ãã
è¦ããã«ãããã³ãã¨ã³ãã®ãµã¼ããã¡ãã¦ã¼ã¶ãã¨ã«ãªã¯ã¨ã¹ãããã¬ã¤ã«ã¾ã¨ã¾ã¦ããã¦ãããã¨ãããã¯ã¨ã³ãå¦çãã©ã¯ãªãã ããã¼ãã¨ãããããªæ§æã ã¨æã£ã¦ãã ããã
ããã§ã¯ã¾ãã¯handlerã®ã³ã¼ãããã¿ã¦ã¿ã¾ãããApache2::ProxyAllotãã¨åä»ãã¦ã¿ã¾ããã
package Apache2::ProxyAllot; use strict; use warnings; use Apache2::RequestRec (); use Apache2::RequestIO (); use APR::Table (); use Apache2::Const -compile => qw( OK DECLINED ); use CGI::Cookie; sub handler { # requestãªãã¸ã§ã¯ã my $r = shift; # cookieåå¾ my %cookies = parse CGI::Cookie($r->headers_in->{Cookie}); $cookies{Apache} and my $str = $cookies{Apache}->value() or return Apache2::Const::DECLINED; # cookieæååãæ°å¤å my $allot; for(split(//, $str )){ $allot += unpack("C*",$_); } # ä»»æã®æ°å¤ï¼ããã§ã¯ï¼ï¼ã§å²ã£ããã¾ããæ±ããâå¿ ã0ãã4ã®ããããã«ãªã $allot = $allot % 5; # æ¯ãåãå webãµã¼ãã決å®ï¼192.168.1.0 ãã 4 ã®ããããï¼ my $webserver = 'http://192.168.1.' . $allot; my $uri = $webserver . $r->uri; # ApacheãPROXYçµç±ã®ãªã¯ã¨ã¹ãã§ããã¨æãããããã®ç´°å·¥ $r->proxyreq(1); # æ°ããURIããªã¯ã¨ã¹ããªãã¸ã§ã¯ãã«ã»ãã $r->uri($uri); # mod_proxyã®ããã®ç¹å¥ãªãã¡ã¤ã«å½¢å¼ $r->filename("proxy:$uri"); # mod_proxyã«å¾ã®å¦çãä»»ããã®ã§handlerãæå®ãã $r->handler('proxy-server'); return Apache2::Const::OK; } 1;
è¦æ £ããªãã¨ã¡ãã£ã¨æ°æã¡æªãæãã¾ãããmod_perl2ã§ã¯ããã¤ãã®Apacheã¢ã¸ã¥ã¼ã«ãå¥ã ã«useããªããã°ãªãã¾ãããmod_perl1ç³»ã®æã¯ãã£ã¨ãã£ãããã¦ãããã§ãããããããç´ä½æ²æ(å§å©ï¼)ããã£ã¦ãããªã£ãããã§ãã
ã³ã¼ãã®ä¸èº«ã¯è¦ã¦ã®éãã§ããã³ã¡ã³ããæ¸ãã¦ãããã®ã§è§£èª¬ã¯çç¥ï¼
ã§ãã®ãã³ãã©ã¢ã¸ã¥ã¼ã«ãApacheã®ã©ã®ãã§ã¼ãºã«ããã¯ããããã§ãããå®ã¯å½åã¯uri_translateãã§ã¼ãºãèãã¦ããã®ã§ãããCookieãã¼ã¿ããã³ããªã³ã°ããã¨ãªãã¨ããã®ï¼ã¤å¾ã®HeaderParseãã§ã¼ãºãããªãã¨ã ããªãã¨ããããã¾ãããCookieã®å¦çãã¯ãã¾ãã®ãHeaderã®è§£æãã§ã¼ãºãããªãã§ããããªãã»ã©ã
ãããApacheå´ã®è¨å®ã«ãªãã¾ãã
PerlRequire /var/www/perl/startup.pl PerlHeaderParserHandler +Apache2::ProxyAllot
PerlRequireã§å¼ã³åºãã¦ããstartup.plã¯ãåã«ä¸ã§use libã§ã¢ã¸ã¥ã¼ã«ãããããã£ã¬ã¯ããªãèªã¿è¾¼ãã§ããã ãã§ãã
大äºãªã®ã¯ãã®æ¬¡ã®è¡ã§ããå
ã»ã©ä¸ã§ä½ã£ãApacheã¢ã¸ã¥ã¼ã«ãããã§è¨å®ãã¾ããPerlHeaderParserHandlerã§æå®ãã¦ããã®ã§ãããã¯HederParseãã§ã¼ãºã«ã¦å¼ã³åºããããã¨ã«ãªãã¾ãã
ãã¨å¿ãã¦ã¯ãããªãã®ã¯mod_proxyã§ãããProxyRequest Onã«ãã¾ãã
<IfModule mod_proxy.c> ProxyRequests On <Proxy *> Order deny,allow Allow from all </Proxy> </IfModule>
ããã§çãã©ããmod_proxyã«ç¬èªã®æ¯ãåããã¸ãã¯ãæè¼ãããã¨ãã§ãã¾ããã
å®éã«åããã¦ã¿ãã¨ãProxyãã¦ãã ããªã®ã§å½ç¶ãªãã§ãããåä½ã¯è¶ é«éã§ããããã£ãã