Our web server has been migrated to a new machine. While Apache 1.3 on the old box had little or no trouble dealing with a series of rewriterules, Apache 2.0 on the new box generates huge loads performing the same task.
Removing the rewriterules brings our load back to acceptable levels.
Expected result:
An URL of the format [myserver.com...]
returns the image /cam_5.jpg or /notallowed.jpg - depending on the requesting IP address and time of the request.
Failing approach:
Our httpd.conf looks like this:
RewriteLock /usr/local/etc/apache2/rewrite.lock
<VirtualHost aaa.bbb.ccc.ddd:80>
RewriteEngine On
RewriteMap parse-token prg:/usr/local/etc/apache2/parsetoken.pl
RewriteRule ^/([A-F0-9]{24})+/(cam_+[1-6]+\.jpg)$ /$2/$1%{REMOTE_HOST}
RewriteCond ${parse-token:$2} <240
RewriteRule ^/(cam_+[1-6]+\.jpg)+/(.*)$ /$1 [L,S=1]
RewriteRule ^/(.*)+\.jpg http://my_other_server.com/notallowed.jpg [L,R]
RewriteRule ^/([A-F0-9]{24})+/(cam_+[1-6]+\.jpg)$ /$2
</VirtualHost>
And the script is:
#!/usr/bin/perl
#
# Apache mod_rewrite script
# Parse time / ip token - determine whether
# to grant access to webcam images
$¦ = 1; # Turn off buffering
while (<STDIN>) {
$tokenIp='';
$tokenTime = substr($_,0,8);
for ($n=8;$n<=20;$n+=4){
$temp = hex(substr($_,$n,2));
$tokenIp .= sprintf('%d', $temp) . '.';
}
$tokenIp = substr($tokenIp,0,-1);
$reqIp = substr($_,24,-1);
$index = rindex($tokenIp,'.') -1;
$tokenIp = substr($tokenIp,0,$index);
$reqIp = substr($reqIp,0,$index);
$myTime = time();
$tokenTime = hex($tokenTime);
$tokenTime = sprintf('%d', $tokenTime);
$timeDiff = int(abs($myTime - $tokenTime) / 60); # no seconds needed
if ($timeDiff > 999) {
$timeDiff = 999;
}
$outString = $timeDiff;
while (length $outString <3){
$outString = '0' . $outString;
}
if ($tokenIp ne $reqIp){
$outString = '999';
}
print $outString;
print "\n"
}
My perl-fu is way rusty, optimizations are greatly appreciated, but the old box handled everything "just fine"...
Now I see the following within mere seconds:
me@host ~ : w
5:54PM up 18 days, 23:35, 3 users, load averages: 0.36, 0.27, 0.25
me@host ~ : w
5:54PM up 18 days, 23:35, 3 users, load averages: 1.05, 0.41, 0.31
me@host ~ : w
5:55PM up 18 days, 23:36, 3 users, load averages: 2.78, 0.97, 0.51
Can anyone nudge me in the right direction?
Norbert