hosting a rails app under a subfolder

Intro

Sometimes it is nice to mount a rails app under subfolder. In fact, I had to do this just today and let me tell you, it wasn't easier to crack this nut. Without further adieu:

The Steps

In config.ru:


      map '/DIRECTORY_NAME' do
        require ::File.expand_path('../config/environment',  __FILE__)
        run RAILS_APP_NAME::Application
      end
      

This makes the rails router happy.

In config/application.rb:


      ENV['RAILS_RELATIVE_URL_ROOT'] = '/DIRECTORY_NAME'
      

This makes the path helpers generate the correct URLs. Not sure if it matters, but I put this at line 1.

nginx

As I run all of my apps through nginx, this is how I had to setup my server mapping:


      server {
          listen 80;
          server_name SERVER_NAME;
          location /DIRECTORY_NAME {
              alias PATH_TO_APP/current/public;
              passenger_enabled on;
          }
      }
      

I use alias here because root would pass along DIRECTORY_NAME when looking for static assets. This means it will look in PATH_TO_APP/current/public/DIRECTORY_NAME/javascript/jquery.js instead of PATH_TO_APP/current/public/javascript/jquery.js.