Deploying Lots of Rails Apps to the Same Domain

Hi. I have a dedicated server (linux / rails / mongrel) that I’m
deploying to. I am able to deploy different applications to different
domains/subdomains just fine using the standard rails / mongrel /
apache config (as instructed from agile web second edition).

However, the kink is I need to deploy lots of rails applications under
the same domain. The reason for this is the applications need to be
directly callable from the parent application (at the root domain) via
AJAX so they need to be on the same domain. Calling an ajax request on
sub-domains fail. I know there are a bunch of javascript hacks, but
for now I’d like to just deploy the apps to the same domain (using
virtual folders to identify them).

Here is what I’m after:

http://[domain] → Rails Parent App (mongrel ports 8000-8001)
http://[domain]/app1/ → Rails Child App 1 (mongrel ports 8010-8011)
http://[domain]/app2/ → Rails Child App 2 (mongrel ports 8020-8021)
etc…

The mongrel instances are set up fine for all my apps. I just need to
fix up the mod_rewrite in order to forward the requests based on
folder name to different mongrel instances. I don’t mind putting the
child app names and paths statically into the config for now.

What I have tried so far is using a rewrite rule that picks the
http://[domain]/app1/[rails path] and forwards it to the mongrel
cluster defined by app1_cluster.
RewriteRule ^/(.)/app1/(.)$ balancer://app1_cluster%{REQUEST_URI}
[P,QSA,L]

Needless to say, it doesn’t work. I don’t think the RewriteRule is
being hit because I cant get the /app1/ requests to hit the right
mongrel cluster. All I get is 404s. The parent rails app works fine at
the root of the domain. I’m guessing my RewriteRule is probably way
off.

Thanks in advance for any help you can offer.

-Jacques
[email protected]

Here is my full apache virtual server config file. Its mostly standard
issue from: http://mongrel.rubyforge.org/docs/apache.html

<VirtualHost 211.149.251.41>
ServerName dev.you-comics.com

DocumentRoot /var/www/apps/youcomics/current/public

<Directory “/var/www/apps/youcomics/current/public”>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all

Configure Mongrel Cluster

<Proxy balancer://youcomics_cluster>
BalancerMember http://127.0.0.1:8010
BalancerMember http://127.0.0.1:8011

<Proxy balancer://app1_cluster>
BalancerMember http://127.0.0.1:8020
BalancerMember http://127.0.0.1:8021

RewriteEngine On

Prevent access to .svn directories

RewriteRule ^(.*/)?.svn/ - [F,L]
ErrorDocument 403 “Access Forbidden”

Check for maintenance file and redirect all requests

RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]

Rewrite index to check for static

RewriteRule ^/$ /index.html [QSA]

Rewrite to check for Rails cached page

RewriteRule ^([^.]+)$ $1.html [QSA]

Redirect all non-static requests to cluster

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

#app1
RewriteRule ^/(.)/app1/(.)$ balancer://app1_cluster%{REQUEST_URI}
[P,QSA,L]

#main dashboard
RewriteRule ^/(.*)$ balancer://youtcomics_cluster%{REQUEST_URI}
[P,QSA,L]

Deflate

AddOutputFilterByType DEFLATE text/html text/plain text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

ErrorLog logs/youcomics.com-error_log
CustomLog logs/youcomics.com-access_log combined

-----

I do something similar. I can’t really help you with the
mod_proxy_balance part as I’m using Apache 2.0. Here’s how I run it;

Apache → nginx → mongrel_cluster
each nginx/mongrel stack has a port different port and I spawn the
mongrels with the --prefix=/foo option.

Apache mod_proxy looks like this
ProxyPass /app1/ http://hostname:8000/app1/
ProxyPassReverse http://hostname:8000/app1/ /app1/

ProxyPass /app2/ http://hostname:8100/app2/
ProxyPassReverse http://hostname:8100/app2/ /app2/

et al.

I don’t use an application bound to / just a static landing page for
simplicity and speed.

hope this helps or at least gives you a direction to try…

–R