• Skip to primary navigation
  • Skip to main content

Inodes

Fractional CTO Consulting

  • Home
  • About Us
  • Contact Us
  • Block Examples
  • Landing Page
  • Pricing Page
  • Show Search
Hide Search

apache

Mongrel, rails and the theory of relativity

John Ferlito · 4 April 2007 · 5 Comments

Summary (E = mc²)

When using mongrel for rails and you want to deploy an app under /other_url then use

Ruby
ActionController::AbstractRequest.relative_url_root = "/other_url"

in config/environments/production.rb instead of

Ruby
ENV['RAILS_RELATIVE_URL_ROOT'] = "/other_url"

Proof (From first principals)

At Vquence we have a pretty standard rails setup

  • Apache with mod_proxy
  • pen
  • mongrel

Silvia recently wrote an application to allow us to edit the news articles posted to our corporate website. I wanted to do something I thought would be pretty simple, have the application appear at /news on our admin web server.

Step one was the obvious change to mod_proxy

Apache
ProxyPass /news http://localhost:8000
ProxyPassReverse /news http://localhost:8000

Of course the problem is that the rails app still thinks it is living on / so it returns URLs like /stylesheets/moo.css instead of /news/stylesheets/moo.css.

A bit of googling found a few email threads with a common solution. In your environment.rb set

Ruby
ENV['RAILS_RELATIVE_URL_ROOT'] = "/other_url"

This is where things fell apart fairly quickly. I could not get this to work no matter what I tried. After a few hours of following a HTTP request through the whole Mongrel and rails stack I discovered the following.

Setting RAILS_RELATIVE_ROOT will work fine if you are running rails using CGI. For the simple reason, which should have been more obvious to me sooner, that CGIs use environment variables to access their parameters. This can be seen in the
ruby CGI class

/usr/lib/ruby/1.8/cgi.rb:

Ruby
class CGI
  def env_table
    ENV
  end
end

However mongrel overloads env_table and does the following instead

/usr/lib/ruby/1.8/mongrel/cgi.rb:

Ruby
class CGIWrapper < ::CGI
  # Used to wrap the normal env_table variable used inside CGI.
  def env_table
    @request.params
  end
end

This makes sense since the rails code is now running inside the web server so environment variables aren’t necessary. Upon investigation I found that the URL morphing magic is performed with rails as follows.

/usr/share/rails/actionpack/lib/action_controller/request.rb:

Ruby
class AbstractRequest
  cattr_accessor :relative_url_root
  
  # Returns the path minus the web server relative installation directory.
  # This can be set with the environment variable RAILS_RELATIVE_URL_ROOT.
  # It can be automatically extracted for Apache setups. If the server is not
  # Apache, this method returns an empty string.
  def relative_url_root
    @@relative_url_root ||= case
      when @env["RAILS_RELATIVE_URL_ROOT"]
        @env["RAILS_RELATIVE_URL_ROOT"]
      when server_software == 'apache'
        @env["SCRIPT_NAME"].to_s.sub(//dispatch.(fcgi|rb|cgi)$/, '')
      else
        ''
    end
  end
end

What this all means is that you can solve the whole problem by placing the following in your config/environments/production.rb

Ruby
ActionController::AbstractRequest.relative_url_root = "/other_url"

Now if only Einstein had put his theories to good use and invented a time machine then maybe I could get the last 4 hours of my life back 🙂

Update: Make sure /other_url isn’t the same name as one of your controllers or bad things happen.

Hit the ground running with a minimalist look. Learn More

Copyright © 2025 · Inodes Pty Ltd · Log in

  • Privacy Policy