Multiple Rails apps within one Apache VirtualHost

Has anyone configured multiple Rails apps within the same VirtualHost
directive? I need to do this with the apps selected by the initial
path. I'm using Apache 2.2.3 with mod_proxy_balancer and Rails 2.0.2.

In fact, non-Rails apps have to be configured within this VirtualHost
also, but that problem has been solved via a Location directive. I
tried a similar Location statement for the Rails apps with no success
yet, so I thought I'd see if anyone has already done this. To date,
I've only needed to configure one Rails app within a VirtualHost with
what appears to be a fairly typical config.

Any info sources for more advanced Apache configuration for Rails
would be appreciated.

Brian Adkins

Brian,

I would look at using mod_proxy to send different URLs to your rails
apps if you need to do in one virtual host. You would start each app
with mongrel_rails on X port and then config Apache like so:

#mongrel rails app#1 running on 3000
ProxyPass /yourapp_prefix http://<yourserver>:3000
ProxyPassReverse /yourapp_prefix http://<yourserver>:3000

#mongrel rails app#1 running on 3001
ProxyPass /yourapp2_prefix http://<yourserver>:3001
ProxyPassReverse /yourapp2_prefix http://<yourserver>:3001

The real issue you will run into with a VPS is memory usage. Some VPSs
will kill your mongrel's if they hog resources. So you'll want to also
run a cron script or "monit" to keep an eye on them.

Brian,

I would look at using mod_proxy to send different URLs to your rails
apps if you need to do in one virtual host. You would start each app
with mongrel_rails on X port and then config Apache like so:

#mongrel rails app#1 running on 3000
ProxyPass /yourapp_prefix http://<yourserver>:3000
ProxyPassReverse /yourapp_prefix http://<yourserver>:3000

I've seen some folks using the above (ProxyPass ProxyPassReverse), and
others using the following (RewriteRule). Any advantages of one over
the other?

# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/app1(.*)$ balancer://cluster1%{REQUEST_URI} [P,QSA,L]
RewriteRule ^/app2(.*)$ balancer://cluster2%{REQUEST_URI} [P,QSA,L]

#mongrel rails app#1 running on 3001
ProxyPass /yourapp2_prefix http://<yourserver>:3001
ProxyPassReverse /yourapp2_prefix http://<yourserver>:3001

The real issue you will run into with a VPS is memory usage. Some VPSs
will kill your mongrel's if they hog resources. So you'll want to also
run a cron script or "monit" to keep an eye on them.

This is on a dedicated machine, not a VPS. The reason I need multiple
Rails apps configured in the same VirtualHost is because we're already
using the host name for another purpose - to identify an
organization. We've specified a * wildcard DNS entry to allow
handling any number of hosts (the apps will parse the hostname to
determine organization). For example:

http://<orgname>.foo.com/rails1/controller/action/...
http://<orgname>.foo.com/rails2/controller/action/...
http://<orgname>.foo.com/nonrails1/...
http://<orgname>.foo.com/nonrails2/...

The apps are written by one company and cooperate, but also have a
degree of independence. I'd prefer to have each app provide its own
location for static files for Apache to serve directly, so I think my
main problem is finding a nice way to have the equivalent of a docroot
per app. I could probably live with a solution that requires the rails
app to share a directory tree - maybe via soft links.

The following solution seems to work fairly well for me so far:

http://tonyrose023.blogspot.com/2007/01/multiple-rails-apps-with-mongrel.html

I had to mess around with some additional RewriteRules to get Apache
to serve static files instead of mongrel and to deal with Rails
caching.