Deploying rails application with pound as a Balancer

Published on Author Akhil Bansal5 Comments

Now a days Apache + mod_proxy + mongrel_clusters, Lighttpd + Mongrel cluster and Nginx + mongrel cluster are well known for deploying rails applications.

You can also deploy your rails application with pound(a reverse proxy, load balancer and HTTPS front-end for Web server).

First you need to setup mongrel_clusters for your rails application by issuing ” mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 3 -c ./ ” inside the rails application root directory(on client machine). This will create mongrel_cluster.yml in config directory. You can change parameters in this command, as -p 8000 specifies that mongrel instances will start up on port number starting 8000, -a 127.0.0.1 specifies that mongrel instances will listen to the localhost, -N specifies the number of mongrel instances and -c specifies the rails root directory.

Now you need to install pound(if not installed) by issuing following commands(as root):

  • cd /opt/src
  • wget http://www.apsis.ch/pound/Pound-2.3.2.tgz
  • tar xzpf Pound-2.1.6.tgz
  • cd Pound-2.1.6
  • ./configure
  • make
  • make install

This will install pound in /usr/local/sbin/pound. In order to proceed further we need to create pound.cfg(pound configuration file) in /etc/pound/pound.cfg . Below is the content of pound.cfg:


User "pound"
Group "pound"

ListenHTTP
  Address 0.0.0.0
  Port 80
  Service
    BackEnd
      Address 127.0.0.1
      Port 8000
    End
    BackEnd
      Address 127.0.0.1
      Port 8001
    End
    BackEnd
      Address 127.0.0.1
      Port 8002
    End
  End
End

Start mongrel cluster by issuing mongrel_rails cluster::start in you app root directory, start pound by /usr/local/sbin/pound -f /etc/pound/pound.cfg , now you are done. Pound is listening the port 80 and redirect all requests to mongrel instances running on 8000, 8001, 8002.

* Please Note that we have configured pound at port 80, if port 80 is being used by apache or any other application pound will not start. You need to stop any service using port 80, if it is apache then stop apache, change line ‘Listen 80’ to “Listen 8080” and start apache.

In a specific case, when apache is running at some port (let say 8080), you may want to use apache to serve static content of your application, in order to reduce some load from mongrels. In that case use the following:


User "pound"
Group "pound"

ListenHTTP
  Address 0.0.0.0
  Port 80
  Service
      URL "/(images|stylesheets|flash|javascripts)/"
      BackEnd
          Address 127.0.0.1
          Port    8080
      End
      Session
          Type    BASIC
          TTL     300
      End
  End
  Service
    BackEnd
      Address 127.0.0.1
      Port 8000
    End
    BackEnd
      Address 127.0.0.1
      Port 8001
    End
    BackEnd
      Address 127.0.0.1
      Port 8002
    End
  End
End

This will redirect all requests for image, stylesheets, javascripts, flash to apache. Now we need to configure apache to serve those static content. Just add a virtualhost for that:

<virtualHost *:8080>
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/html/example.com/public
  <directory "/var/www/html/example.com/public" >
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
  </directory>

  RewriteEngine On

  # For static files it's good to avoid hitting your mongrel process
  # so let apache knows it should serve it directly
  # for a rails application it means, files in images / stylesheets / javascripts
  RewriteRule "^/(images|stylesheets|flash|javascripts)/?(.*)" "$0" [L]

</virtualHost>

Its all done. All requests for dynamic content at port 80 will be redirect to mongrel running at 8000, 8001, 8001 and requests for static content will be served by apache running at port 8080.

5 Responses to Deploying rails application with pound as a Balancer

Leave a Reply

Your email address will not be published. Required fields are marked *