YAPC::Asia Tokyo 2010 㧠LT ãã¦ãã¾ããã以ä¸ã¯ãã®è³æ(ã«å°ã説æã追å ãããã®)ã§ãã
mod_rewrite
- æ£è¦è¡¨ç¾ã«ããURLæ¸ãæãã¢ã¸ã¥ã¼ã«
- ã¹ã¤ã¹è£½ã¢ã¼ãã¼ãã¤ã / é»éè¡
- ã¾ã Apache 使ã£ã¦ã¾ããã?
- reverse proxy ã¨ãâ¦
ã¯ã¦ãªã® mod_rewrite æ´»ç¨äºä¾
- ã»ã¼ reverse proxy
- URLã«ããç¨éå¥ã®backendã«æ¯ãåãã
- ç¨éã«ããbackendãåããªã½ã¼ã¹å¹çå
- ç¹å®ã®ã¢ã¯ã»ã¹ããã£ãã·ã¥ãµã¼ãã¼ã«æ¯ã
- URLå å·¥
- Squidã«ãã£ãã·ã¥ãããããåä¸URLã§ç°ãªãã³ã³ãã³ããè¿ãå ´åããã
- âã¯ã¨ãªã«æ å ±ãä»å ãã
- BAN!
便å©ãªåé¢â¦
- å¢ãã!
$ cat jp.www.proxy.apache.conf | grep Rewrite | wc -l 179
- ãã¹ããã¥ãã! â ä¸è¡å ããã®ãæãããâ¦
Test::Apache::RewriteRules
ããã§ã Test::Apache::RewriteRules ã
ã¯ã¦ãªç¤¾å 製ãã¹ãã¢ã¸ã¥ã¼ã« (id:wakabatanä½æ) ã§ãApache ã® RewriteRule ã®åä½ããã¹ãã§ãã¾ãã
ãã¡ãã§å
¬éä¸ã§ã â http://github.com/hatena/perl5-test-apache-rewriterules
ããã¿
ç°å¢è¨å®
- Apache2 ãå ¥ãã
- mod_ssl ãªã©å¿ è¦ã¢ã¸ã¥ã¼ã«ãå ¥ãã
SYNOPSIS
ãã㪠apache.conf ããã£ãã¨ãã¦
RewriteRule /foo/(.*) http://%{ENV:ReverseProxyedHost1}/$1 [P,L] RewriteRule /bar/(.*) http://%{ENV:ReverseProxyedHost2}/$1 [P,L] RewriteRule /hoge/(.*) http://external.test/$1 [R,L]
ãããªãã¹ããæ¸ãã¾ãã
use Test::Apache::RewriteRules; use Path::Class qw/file/; my $apache = Test::Apache::RewriteRules->new; $apache->add_backend(name => 'ReverseProxyedHost1'); $apache->add_backend(name => 'ReverseProxyedHost2'); $apache->rewrite_conf_f(file('apache.rewrite.conf')); $apache->start_apache; $apache->is_host_path('/foo/a', 'ReverseProxyedHost1', '/a'); $apache->is_host_path('/bar/b', 'ReverseProxyedHost2', '/b');, $apache->is_redirect('/hoge/z', 'http://external.test/z'); $apache->stop_apache;
è¨å®ãã¡ã¤ã«ã®æ¸ãæã
é常ãInclude ãã£ã¬ã¯ãã£ããªã©ã§ãã«ãã¹ãæå®ãã¦ããã®ã§ãè¨å®ãã¡ã¤ã«ãã³ãã¼ãã¦æ¸ãæãã¾ãã
æåå or æ£è¦è¡¨ç¾
â
æåå or ã³ã¼ãåç §
æ¸ãæãä¾
my $copied_conf = $apache->copy_conf_as_f($org_file, [ "foo" => "bar", "hoge" => sub { "fuga" }, qr/(foo|bar)/ => "baz$1", ]);
- VirtualHost ã CustomLog ãªã©é¢ä¿ãªããã£ã¬ã¯ãã£ããå ¨é¨æ¶ãã¾ã
- RewriteRule ã ãã® conf ãã¡ã¤ã«ã ã¨ç°¡åã§ã
apache.conf ãã»ãã
$apache->rewrite_conf_f($copied_conf);
Apache ã®èµ·åã«å¿ è¦ãªãã£ã¬ã¯ãã£ããªã©ã¯ã¢ã¸ã¥ã¼ã«ãåæã«ç¨æãã¾ãã
ããã¯ã¨ã³ãã®ç»é²
ãªãã¼ã¹ãããã·å ã®ããã¯ã¨ã³ããç»é²ãã¾ãã
$apache->add_backend(name => 'BackendMainVIP'); $apache->add_backend(name => 'BackendBotVIP'); $apache->add_backend(name => 'SquidVIP');
RewriteRule 㧠%{ENV:BackendMainVIP} ã®ããã«ç°å¢å¤æ°ã使ãã
å®éã® apache.conf ã§ãã®ãããªç°å¢å¤æ°ã使ã£ã¦è¨è¿°ãã¦ããªãæã¯ã copy_conf_as_f ã§æ¸ãæãã
Apache ã®èµ·å
$apache->start_apache;
ãã¹ã (1) is_host_path
$apache->is_host_path('/' => 'BackendMainVIP', '/');
ãã¹ã (2) is_redirect
$apache->is_redirect('/foo' => 'http://example.com/bar');
- is_redirect .. ãªãã¤ã¬ã¯ãã®ãã¹ã
- ã¢ã¯ã»ã¹ãã path
- ãªãã¤ã¬ã¯ãå ã® URL
Test::Apache::RewriteRules::ClientEnvs
ç¹å®ã®ã¯ã©ã¤ã¢ã³ãç°å¢ãåç¾ãããããã¯ãæä¾ãã¾ãã
with_docomo_browser { $apache->is_redirect('/n/' => 'http://n/mobile/?&guid=on'); }; with_request_method { $apache->is_host_path('/' => 'BackendMainVIP', '/'); } 'POST';
ç¾å¨ãµãã¼ããã¦ããç°å¢
Apache ã®åæ¢
$apache->stop_apache; # (DESTROY ã§ãå¼ã°ãã¾ãã)