Nepal on Rails

millisami's rants!

Setup HAProxy for Development Environment on Mac OSX for Rails App


  1. You know what HAProxy is
  2. HomeBrew installed
  3. A rails app to test against


Simple and easy to install HAProxy. Just the following commands:

brew update
brew install haproxy

Configuring haproxy config file inside the rails app

Add the following file inside config/haproxy.cfg

  maxconn 4096
  pidfile ~/tmp/

  log global
  log local0
  log local1 notice
  mode http
  timeout connect 300000
  timeout client 300000
  timeout server 300000
  maxconn 2000
  option redispatch
  retries 3
  option httpclose
  option httplog
  option forwardfor
  option httpchk HEAD / HTTP/1.0

frontend http-farm
  bind *:9000
  default_backend app1latest
  acl url_tag02 path_beg /tag02/
  use_backend tagged-02 if url_tag02

backend app1latest
  balance roundrobin
  server localhost_9001 localhost:9001

backend tagged-02
  balance roundrobin
  server localhost_9002 localhost:9002

listen haproxyapp_admin:9100
  mode http
  stats uri /

Testing out

Now to test this architecture, fire-up 4 terminals. Run the following commands within the rails app directory

Terminal 1: haproxy -f config/haproxy.cfg

This will run the haproxy and you can view its metrics at http://localhost:9100

Terminal 2: bundle exec rails s -p 9001

Terminal 3: bundle exec rails s -p 9002

The above 2 commands fire up two separate backend servers which will get the request from haproxy

And lastly, the following command with send the request at []( Do hit couple of times. You’ll see how haproxy balance the requests and send it over the 2 backend servers.

Terminal 4: curl [](

Now you can play around with haproxy and your rails app with different configurations. The configurations are endless as per your needs.

Note: The application I’m working on has a different requirement to run the application separately on different servers once the new code changes is pushed to the main application for certain time. Its like running the different versions of the same application until the pending jobs are done. So, the strategy I took is to use load-balancer (haproxy) to determine to which server farm to send based off of the url substring. I will post the detailed explanation of the architecture in future. So, if you want to stay tuned, you can follow @nepalonrails