<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>John&#039;s Tidbits &#187; apache</title>
	<atom:link href="http://inodes.org/tag/apache/feed/" rel="self" type="application/rss+xml" />
	<link>http://inodes.org</link>
	<description>Moo - Development, Trouble-shooting and Random thoughts...</description>
	<lastBuildDate>Thu, 07 Apr 2011 11:38:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Mongrel, rails and the theory of relativity</title>
		<link>http://inodes.org/2007/04/04/mongrel-rails-and-the-theory-of-relativity/</link>
		<comments>http://inodes.org/2007/04/04/mongrel-rails-and-the-theory-of-relativity/#comments</comments>
		<pubDate>Wed, 04 Apr 2007 05:15:06 +0000</pubDate>
		<dc:creator>johnf</dc:creator>
				<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mogrel]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[vquence]]></category>

		<guid isPermaLink="false">http://inodes.org/blog/2007/04/04/mongrel-rails-and-the-theory-of-relativity/</guid>
		<description><![CDATA[Summary (E = mc&#178;) When using mongrel for rails and you want to deploy an app under /other_url then use ActionController::AbstractRequest.relative_url_root = "/other_url" in config/environments/production.rb instead of 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 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary (E = mc&sup2;)</strong></p>
<p>When using mongrel for rails and you want to deploy an app under /other_url then use</p>
<pre>
    ActionController::AbstractRequest.relative_url_root = "/other_url"
</pre>
<p>in config/environments/production.rb instead of</p>
<pre>
    ENV['RAILS_RELATIVE_URL_ROOT'] = "/other_url"
</pre>
<p><strong>Proof (From first principals)</strong></p>
<p>At <a href="http://www.vquence.com">Vquence</a> we have a pretty standard rails setup</p>
<ul>
<li>Apache with mod_proxy</li>
<li>pen</li>
<li>mongrel</li>
</ul>
<p><a href="http://blog.gingertech.net">Silvia</a> 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.</p>
<p>Step one was the obvious change to mod_proxy</p>
<pre>
    ProxyPass /news http://localhost:8000
    ProxyPassReverse /news http://localhost:8000
</pre>
<p>Of course the problem is that the rails app still thinks it is living on <em>/</em> so it returns URLs like <em>/stylesheets/moo.css</em> instead of <em>/news/stylesheets/moo.css</em>.</p>
<p>A bit of googling found a few email threads with a common solution. In your environment.rb set</p>
<pre>
    ENV['RAILS_RELATIVE_URL_ROOT'] = "/other_url"
</pre>
<p>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.</p>
<p>Setting <em>RAILS_RELATIVE_ROOT</em> 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<br />
ruby CGI class</p>
<p>/usr/lib/ruby/1.8/cgi.rb:</p>
<pre name="code" class="ruby">

class CGI

def env_table
    ENV
end
</pre>
<p>However mongrel overloads <em>env_table</em> and does the following instead</p>
<p>/usr/lib/ruby/1.8/mongrel/cgi.rb:</p>
<pre name="code" class="ruby">

class CGIWrapper < ::CGI

    # Used to wrap the normal env_table variable used inside CGI.
    def env_table
        @request.params
    end
</pre>
<p>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.</p>
<p>/usr/share/rails/actionpack/lib/action_controller/request.rb:
</pre>
<pre name="code" class="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
</pre>
<p>What this all means is that you can solve the whole problem by placing the following in your <em>config/environments/production.rb</em></p>
<pre name="code" class="ruby">
    ActionController::AbstractRequest.relative_url_root = "/other_url"
</pre>
<p>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 <img src='http://inodes.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Update:</strong> Make sure <em>/other_url</em> isn&#8217;t the same name as one of your controllers or <strong>bad</strong> things happen.</p>
]]></content:encoded>
			<wfw:commentRss>http://inodes.org/2007/04/04/mongrel-rails-and-the-theory-of-relativity/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

