HAProxy
HAProxy is a very fast and highly configurable software TCP load balancer. HAProxy is extremely attractive as a reverse proxy in a Rails stack because of it’s unique maxconn 1 configuration setting, allowing the user to specify that each mongrel gets only one request at a time. If you’re still not convinced, note that HAProxy is currently used by 37Signals and GitHub. This guide will help you install and configure HAProxy on your RailsMachine.
Impact to your website
You can configure and test HAProxy without impacting any applications you may already have running. When you’re ready to swap it in, you’ll just need to restart Apache. The downtime to the outside world will be very brief.
Getting HAProxy
The first step is to download, build and install HAProxy. We highly recommend version 1.3.15.4 or later.
wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.15.4.tar.gz
tar xvzf haproxy*
cd haproxy*
make TARGET=linux26
cp haproxy /usr/
sudo install --owner=root --group=root -m700 haproxy /usr/sbin/
Configuring the proxy server
Download this configuration file. At the bottom of the file, edit the listen section to use the correct mongrel ports. If you have more than one application, you can add additional listen blocks. For each additional listen block, increment the haproxy listen port (9000,9001,9002,etc) and adjust the mongrel ports for the application (8000, 8010, 8020,etc).
Save the configuration to your server as /etc/haproxy.conf.
Now save this init script to /etc/init.d/haproxy, then run:
sudo chmod +x /etc/init.d/haproxy
sudo chkconfig haproxy on
sudo /sbin/service haproxy start
Test your configuration before you continue (you might need to ‘yum install -y curl’):
curl localhost:9000
You should see the HTML from the root URL of your application. If not, double check your settings and restart the proxy with sudo /sbin/service haproxy restart until you do.
Configuring Apache
Once things seem to be working, you can update your Apache configuration. This will be located on your server at /etc/httpd/conf/apps/APPNAME.conf. If you configured the application as the default vhost, then it will be at /etc/httpd/conf/default.conf
Be sure to backup your configuration just in case, and then:
- Remove the mod_proxy_balancer that start with:
<Proxy balancer://APPNAME_cluster> - Look for this line:
RewriteRule ^/(.*)$ balancer://APPNAME_cluster%{REQUEST_URI} [P,QSA,L] - And change it to this:
RewriteRule ^/(.*)$ http://localhost:7001%{REQUEST_URI} [P,QSA,L]
Be sure to use the correct listen port for your application. In the haproxy configuration template, we use 8001 for the first application.
Restart Apache to reload the configuration.
sudo /sbin/service httpd restart
Proxy Statistics
HAProxy provides a status page showing the health of your mongrels. To see your status page, visit http://yourapp.com/haproxy?stats. The username/password for the stats page are haproxy/stats in our configuration template – feel free to tweak this to your liking.
Health Checks
In the provided configuration template, HAProxy will attempt to load the home page of your application from each mongrel once every twenty seconds to verify that each mongrel is available for new requests. If your home page is cached or doesn’t perform very well, you may perfer to install pulse. Pulse is a Rails plugin that provides a lightweight action intended soley for health checks. Once you install pulse, uncomment the following line in your HAProxy config:
option httpchk GET /pulse
Restart HAProxy to reload the configuration.
sudo /sbin/service haproxy restart
