A few quick notes concerning Mongrel load balancing setup behind an Apache proxy server. These are a few gotcha's I encountered that should be shared with the rest of the webosphere (hopefully if you have the same problem you'll be directed here for a quick bit of help).
First, I am assuming the following: You have apache2 up and running, and you are trying to proxy requests to mongrel_rails - which you have at least one server running...
If you receive an Apache error stating that you do not have permission to access a particular directory (which normally you do, except when you try to proxy).. and you see in the apache2/error.log
client denied by server configuration: proxy:http://127.0.0.1:8000/
Or something similar... then you need to do the following. Edit apache's proxy.conf which is nicely configured right off the bat to be a pain in the butt:
sudo nano /etc/apache2/mods-available/proxy.conf
Inside you will see something like the following - make yours match this - all I did was comment out the "Deny from all" and add an "Allow from 127.0.0.1" -- this assumes that you are only proxying on the localhost.
<Proxy *>
AddDefaultCharset off
Order deny,allow
#Deny from all
Allow from 127.0.0.1
</Proxy>
Extra mongrel_cluster notes
Next lets cover a quick tutorial (more for my benefit than anything else) on exactly how do you setup the mongrel cluster. These are very specific instructions for Ubuntu - sorry I dont have the time to expand these to be very thorough or general, again I assume you already have mongrel_rails in place and working (see my previous post for a gotcha there).
Also - I put a blank line in between shell commands. If there isn't a blank line between something then consider it all to be on one line when you type it into the shell console.
Im also basing this off a particular setup that was quite different from one I had dealt with before - so be warned this may not match exactly what you have.
sudo gem install mongrel_cluster
sudo cp /var/lib/gems/1.8/gems/mongrel_cluster-0.2.1/resources/mongrel_cluster /etc/init.d/mongrel_cluster
sudo chmod +x /etc/init.d/mongrel_cluster
There you just installed mongrel_cluster then took added the mongrel_cluster command to your init folder - and prepared it for on-bootup execution. Now if you look inside the mongrel_cluster file:
nano /etc/init.d/mongrel_cluster
You will see this line:
# Gracefully exit if the controller is missing.
which mongrel_cluster_ctl >/dev/null || exit 0
If you try to run mongrel_cluster at this point (or at least on my system) -
You will not get any output from mongrel_cluster because it cannot find the mongrel_cluster_ctl script
Thats why you will also need to do this:
sudo ln -s /var/lib/gems/1.8/bin/mongrel_cluster_ctl /usr/bin/mongrel_cluster_ctl
Good, almost there - now create a directory in /etc called "mongrel_cluster" (This is the Required name!!!)
sudo mkdir /etc/mongrel_cluster
Now, go into your rails project directory (so that if you were to type ls you would see the app/ config/ log/ public/ etc... folders). Then create your mongrel_cluster config:
sudo mongrel_rails cluster::configure -e development -p 8000 -N 3 -c /var/www/rails_project_directory -a 127.0.0.1 --user www-data --group www-data
This will update the file config/mongrel_cluster.yml in your rails app. You can test it by doing "sudo mongrel_rails cluster::start" -- check that it correctly created your pid files inside the log/ directory - then do a "sudo mongrel_rails cluster::stop".
Now, link that config into your mongrel_cluster directory:
sudo ln -s /var/www/rails_project_directory/config/mongrel_cluster.yml /etc/mongrel_cluster/your_project_name.yml
Finally setup the server to autostart the mongrel_cluster upon reboot:
sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults
So, now your mongrel cluster should boot up and dazzle you with its ease of setup. So - lets take a big look at that VirtualHost setup in your apache config - how, now, do you configure your VHost to support multiple mongrel servers? Not too hard, here is what you will need inside your VHost config to make it properly proxy requests to Mongrel.
ServerName www.mysite.com
ServerAlias mysite.com *.mysite.com
ProxyPass / balancer://mongrel_cluster
ProxyPassReverse / balancer://mongrel_cluster
ProxyPreserveHost on
<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
BalancerMember http://127.0.0.1:8002
</Proxy>
Well, I lied.. I thought this would be a quick post - but it wasn't. I guess it doesn't matter, I wont loose any sleep over it.
Until next time, happy coding =)
