Training, Open Source computer languages

PerlPHPPythonMySQLhttpd / TomcatTclRubyJavaC and C++LinuxCSS

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Clustering, load balancing, mod_rewrite and mod_proxy
"It's not friendly - it's like a reference book". Customer comment on yesterday's tailored training day, where we were load balancing a web application between a number of back end servers, using Apache httpd, with mod_proxy and mod_rewrite to do the clever bits.

Our customers are right - it IS tough for a newcomer to work out what to do from the mod_rewrite manual. So you might like, for starters, to read my article on techniques for load balancing and clustering. Then you might like to look at some of the sample configuration files that we used to have a single httpd instance share the load between multiple further httpds, or multiple Tomcats. Yes, that's right, the solution works not only for Java applications, but also for Perl and PHP ones!

For newcomers to balancing the load, via mod_rewrite and mod_proxy, here's a commented version of the additions we made to httpd.conf at the end of the day, to share PHP applications in a directory called /demo on our backend servers via a URL called /booze on our front end.


# Set up a mapping of server names - see below

RewriteMap SERVERS rnd:/usr/local/apache2/conf/phpbal.conf

# Only make the following tests if the URL starts /booze

<Location /booze>
RewriteEngine On

# Look for a cookie called "what", which is the flag
# we use for a continuing session.

# If the cookie is there, it's been set up to include the
# server name as given in the phpbal file ... and if you
# find it, rewrite the URL to the appropriate server,
# in the demo directory. The [P,L] tells mod_rewrite to
# call mod_proxy for the new URL, and that this is the last
# rewrite in the chain - i.e. if it matches, don't look at
# any further instructions!

RewriteCond %{HTTP_COOKIE} "what=(\w+)-(\w+)"
RewriteRule /booze/(.*) http://${SERVERS:%1}/demo/$1 [P,L]

# If there was no cookie, however, this is the first request
# of a series and it should be forwarded to a server in the
# group at random. FIRST is defined in phpbal.conf

RewriteRule /booze/(.*) http://${SERVERS:FIRST}/demo/$1 [P]
</Location>

# Responses from the two servers may (will?) have been
# rewritten to point directly back at the back end server.
# Rewrite those returned URLs so that future requests will
# also be brokered via the front end server, and the back
# end system will be transparent to the user.

ProxyPassReverse /booze http://192.168.200.67:80/demo
ProxyPassReverse /booze http://www.wellho.net:80/demo


The phpbal.conf file is as follows:

local 192.168.200.67:80
live www.wellho.net:80
FIRST 192.168.200.67:80|www.wellho.net:80


and that instructs the server to randomly choose between our live web server and one of the servers in our training centre for first service ... then to revert to the same server if the cookie is set with a "live" or "local" value. The full source of the PHP application we tested with is in one of the links above, but the vital line to set the cookie is worth reproducing here:

setcookie("what","local-".$uniquekey);

with "local" changed to "live" on the second system in the cluster ...

I do enjoy days like yesterday, when we explored topics that are advanced and not quite the nor for our usual courses. My customer left really happy with what we had done, and I had the opportunity to push through and develop further practical knowledge and further my own understanding of some of the detail - a true win/win. And of course I'm all the better place not if YOU want to come and learn more about sharing the load across servers - whether you've a PHP or Java application.
(written 2006-11-21 07:02:15)

 
Associated topics are indexed under
A900 - Web Application Deployment - Tomcat - Extra Features
A603 - Web Application Deployment - Further httpd Configuration
H114 - Sourcing, installing and configuring PHP

Back to
Course Joining package - updated
Previous and next
or
Horse's mouth home
Forward to
Autumn leaves in Wiltshire - Potterne

Some other Articles
Bratton and Edington new town, Wiltshire
Display an image from a MySQL database in a web page via PHP
Global, Superglobal, Session variables - scope and persistance in PHP
Autumn leaves in Wiltshire - Potterne
Clustering, load balancing, mod_rewrite and mod_proxy
Course Joining package - updated
A tale of a wee wall
Before and After - Well House Manor
-> , >= and => in Perl
Presenting Melksham - for a weekend away in Wiltshire
1638 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho