Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Please help Monks Allmighty. How is this code used in the main site? Is it called by using java within my index.html or what? Here's the public domain code I found. I have it running but what should I see to know it works as advertised?
#!/usr/bin/perl #Load balancing #You've hit the big time, and your site is getting more hits than you +ever dreamed of. #Millions, zillions of hits. What's that? System load just passed 50 a +nd response time is getting kinda' s-l-o-w-w-w? #Perl to the rescue. Set up several replica Web servers with different + hostnames and IP addresses. #Run this script on the ``main'' site and watch it round-robin the req +uests to the replica servers. #It uses IO::Socket to listen for incoming requests on port 80. It the +n changes its privileges to run as #nobody.nogroup, just like a real Web server. Next it preforks itself +a few times (and you always thought #preforking was something fancy, didn't you?), and goes into an accept +() loop. Each time an incoming session comes in, #it forks off another child to handle the request. The child reads the + HTTP request and issues an HTTP redirection to #send the browser to a randomly selected server. #NOTE: Another way to do this is to have multiple ``A'' records define +d for your server's hostname and let DNS caching distribute the load. + #---------------- Script I.4.1: A Load Balancing ``Web Server'' ------ +--- use CGI qw/:standard/; print "Content-type:text/html\n\n"; print "hello", "\n"; # list of hosts to balance between my @HOSTS = qw/www1.mydomain.com www2.mydomain.com www3.mydomain.com w +ww4.mydomain.com/; use IO::Socket; $SIG{CHLD} = sub { wait() }; $ENV{'PATH'}='/bin:/usr/bin'; chomp($hostname = `/bin/hostname`); print $hostname, "\n"; # Listen on port 80 $sock = IO::Socket::INET->new(Listen => 5, LocalPort => 80, LocalAddr => $hostname, Reuse => 1, Proto => 'tcp'); # become "nobody" $nobody = (getpwnam('nobody'))[2] || die "nobody is nobody"; $nogroup = (getgrnam('nogroup'))[2] || die "can't grok nogroup"; ($<,$() = ($>,$)) = ($nobody,$nogroup); # get rid of root privileges! ($\,$/) = ("\r\n","\r\n\r\n"); # CR/LF on output/input # Go into server mode close STDIN; close STDOUT; close STDERR; # prefork -- gee is that all there is to it? fork() && fork() && fork() && fork() && exit 0; # start accepting connections while (my $s = $sock->accept()) { do { $s->close; next; } if fork(); my $request = <$s>; print $request, "\n"; redirect($1,$s) if $request=~/(?:GET|POST|HEAD|PUT)\s+(\S+)/; $s->flush; undef $s; exit 0; } sub redirect { my ($url,$s) = @_; my $host = $HOSTS[rand(@HOSTS)]; print $s, "HTTP/1.0 301 Moved Temporarily", "\n"; print $s, "Server: Lincoln's Redirector/1.0", "\n"; print $s, "Location: http://${host}${url}", "\n"; print $s, "", "\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Load balancer
by arkturuz (Curate) on Jul 30, 2013 at 08:27 UTC | |
Re: Load balancer
by hippo (Archbishop) on Jul 30, 2013 at 08:32 UTC | |
by Anonymous Monk on Jul 31, 2013 at 23:12 UTC | |
by Anonymous Monk on Aug 02, 2013 at 08:38 UTC | |
by Anonymous Monk on Aug 25, 2013 at 04:47 UTC |