Using Apache with Mongrel or Thin and Rails 3

When using mod_proxy to load balance between members of a mongrel or thin cluster it’s important not to pass requests through for static content.  A basic virtual site can be configured like this:

<VirtualHost *:80>

    ServerName myapp.mydomain.com
    DocumentRoot /opt/myapp/public

    <Proxy balancer://thinservers>
        BalancerMember http://127.0.0.1:3000
        BalancerMember http://127.0.0.1:3001
        BalancerMember http://127.0.0.1:3002
    </Proxy>

    ProxyPreserveHost On
    ProxyPass /images !
    ProxyPass /stylesheets !
    ProxyPass /javascripts !
    ProxyPass / balancer://thinservers/
    ProxyPassReverse / balancer://thinservers/

    <Proxy *>
     Order deny,allow
     Allow from all
    </Proxy>

    ErrorLog /var/log/apache2/error.log
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined

</VirtualHost>

5 thoughts on “Using Apache with Mongrel or Thin and Rails 3

  1. Reply
    jeroen - June 30, 2011

    Hi Piers,

    Why do you explicitly list images, stylesheets, javascripts in the ProxyPass directive. They won’t ever get forwarded to Thin since they are real files, as defined in:

    RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f

    or am I missing something?

    1. Reply
      Piers C - June 30, 2011

      The short answer is that’s what works, without the ProxyPass filters the static resources don’t get served. I think ProxyPass / … proxies everything regardless of whether the URL was rewritten. I had to add the ProxyPass filters when I upgraded to Rails 3: I guess Rails 2 served static content by default and that got turned off with the new version.

      1. Reply
        Piers C - June 30, 2011

        Looking at http://www.ruby-forum.com/topic/156523 I realize now it’s the URL rewrite that’s redundant, I’ve removed it from my site configuration and the post above.

  2. Reply
    Donapieppo - February 8, 2012

    With rails 3.1 and asset pipeline use

    ProxyPass /assets !

    instead of the three

    ProxyPass /images !
    ProxyPass /stylesheets !
    ProxyPass /javascripts !

  3. Reply
    Apache, Unicorn & SSL - April 1, 2012

    […] used Apache with Mongrel, Thin and Passenger, I’ve now moved on to Unicorn.  Setting up Apache on Ubuntu to proxy to […]

Leave a Reply to Piers C Cancel reply

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

Scroll to top