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
    How is this code used in the main site?

    When you run it, it becomes your main web server for handling all web requests instead of Apache or something else you use. Make sure none of those other servers are already running and listening on port 80. You will have to edit @HOSTS variable and fill in your hostnames. It doesn't work out of the box.

    but what should I see to know it works as advertised?

    I would run it and try it and see for myself. It might be enough for your situation, or you will have to install something more robust.

Re: Load balancer
by hippo (Archbishop) on Jul 30, 2013 at 08:32 UTC

    It must be set up as the handler on the main site. It is a CGI script.

    It looks like Lincoln Stein's work - are you sure it is public domain? I would contact the author if unsure before getting yourself into a whole heap of legal trouble.

      Ok yes, it is Steins work, thanks for the heads up. All I see is the word "hello" and a white page when I run it. So your saying to create the 4 servers by creating 4 subdomains (www1,www2,www3,www4) that point back to the main site or point to 4 mirror sites. 4 different servers? Thanks for the comments so far :)
        But if a work does not work is it still a work? I'll FIX IT SO THEN IT'S A WORK...