<?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>RuslanY Blog &#187; RubyOnRails</title>
	<atom:link href="http://ruslany.net/tag/rubyonrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ruslany.net</link>
	<description>IIS, FastCGI, PHP and other interesting stuff</description>
	<lastBuildDate>Fri, 26 Aug 2011 06:36:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby on Rails in IIS 7.0 with URL Rewriter</title>
		<link>http://ruslany.net/2008/08/ruby-on-rails-in-iis-70-with-url-rewriter/</link>
		<comments>http://ruslany.net/2008/08/ruby-on-rails-in-iis-70-with-url-rewriter/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 17:57:15 +0000</pubDate>
		<dc:creator>ruslany</dc:creator>
				<category><![CDATA[FastCGI]]></category>
		<category><![CDATA[URLRewrite]]></category>
		<category><![CDATA[RubyOnRails]]></category>

		<guid isPermaLink="false">http://ruslany.net/2008/08/ruby-on-rails-in-iis-70-with-url-rewriter/</guid>
		<description><![CDATA[This article is very out of date at this point and most probably the instructions given here do not work. Please refer to more up-to-date and supported solutions for running Ruby on Rais in IIS 7 provided by Helicon Zoo Web Framework Repository. If you ever tried to set up Ruby on Rails (RoR) on [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>This article is very out of date at this point and most probably the instructions given here do not work. Please refer to more up-to-date and supported solutions for running Ruby on Rais in IIS 7 provided by <a href="http://www.helicontech.com/zoo/">Helicon Zoo Web Framework Repository</a>.</p></blockquote>
<p>If you ever tried to set up Ruby on Rails (RoR) on IIS 7.0 with FastCGI you have probably noticed that the process is not very straightforward. There are a few workarounds that need to be applied in order for RoR to function correctly. In particular, handling of static files in your web application can be tricky on IIS 7.0. The problem is that RoR uses clean URL&#8217;s that look similar to this: <strong>http://mysite.com/home/about</strong>. In order for RoR to be invoked for this kind of URL&#8217;s  it is necessary to create a &#8220;catch all&#8221; handler mapping in IIS (that is a handler mapping with path attribute set to &#8220;<strong>*</strong>&#8220;). When you create such a handler mapping it will cause requests for static files to be routed to RoR, which will obviously fail to handle them.</p>
<p>For RoR to work correctly on IIS with FastCGI it needs to behave like a 404 handler, meaning that it should be invoked only if the requested URL does not exist as a file on a physical file system. The default IIS configuration does not allow this kind of configuration, so there are several workarounds available today &#8211; <a title="Configuring RoR on IIS7" href="http://wiki.rubyonrails.org/rails/pages/HowToConfigureIIS7" target="_blank">this article</a> describes them in great details. However, these workarounds are either not recommended for production usage or are not easy to configure. In this post I will explain how URL Rewrite Module for IIS 7.0 can be used to configure IIS and RoR to work correctly with static files.</p>
<p><span id="more-49"></span>First let&#8217;s setup RoR. To set it up I used instructions from these two great articles:</p>
<ul>
<li>Mike Volodarsky&#8217;s <a title="10 steps to setup RoR" href="http://mvolo.com/blogs/serverside/archive/2007/02/18/10-steps-to-get-Ruby-on-Rails-running-on-Windows-with-IIS-FastCGI.aspx" target="_blank">10 steps to get Ruby on Rails running on Windows with IIS FastCGI</a></li>
<li>Ruby On Rails wiki article <a title="How to configure IIS7" href="http://wiki.rubyonrails.org/rails/pages/HowToConfigureIIS7" target="_blank">How To Configure IIS7</a></li>
</ul>
<p>Both articles are a bit outdated, so I ended up with performing the following steps:</p>
<p><strong>Step 1:</strong> Installed latest version of Ruby by using <a title="Ruby One Click Installer" href="http://www.ruby-lang.org/en/downloads/" target="_blank">One-Click Installer</a>. At the time of writing this post the latest stable version was 1.8.6-26.</p>
<p><strong>Step 2</strong>: Installed Rails by running gem installer from command line window:</p>
<pre name="code" class="cpp">
gem intsall rails --include-dependencies
</pre>
<p><strong>Step 3</strong>: Installed <a title="Ruby for IIS" href="http://avhelp.ucc.usyd.edu.au/james/RubyForIIS-0.1.exe">RoR extensions for IIS</a>. After installation completed I checked that everything was setup successfully by typing the following in the command line window:</p>
<pre name="code" class="css">
C:\&gt;irb
irb(main):001:0&gt; require &#039;fcgi&#039;
=&gt; true
irb(main):002:0&gt; quit
</pre>
<p><strong>Step 4: </strong>Created a web site in IIS with physical path C:\inetpub\ruby and with host name &#8220;ruby&#8221;. Also modified the %WINDIR%\system32\drivers\etc\hosts file to include this line:</p>
<pre name="code" class="cpp">
127.0.0.1 ruby
</pre>
<p><em>Note that if you want to have RoR application in the &#8220;Default Web Site&#8221; in IIS you do not need to do this step.</em></p>
<p><strong>Step 5</strong>: Created a test RoR application in a root folder of a web site:</p>
<pre name="code" class="cpp">
cd c:\inetpub\ruby
rails myapp
cd myapp
ruby script\generate controller test index
</pre>
<p><strong>Step 6</strong>: Created a temporary FastCGI handler mapping for RoR:</p>
<pre name="code" class="xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;configuration&gt;
&lt;system.webServer&gt;
&lt;handlers&gt;
&lt;add name=&quot;Ruby via FastCGI&quot;
path=&quot;*&quot;
verb=&quot;*&quot;
modules=&quot;FastCgiModule&quot;
scriptProcessor=&quot;C:\ruby\bin\ruby.exe|c:\inetpub\ruby\myapp\public\dispatch.fcgi development&quot;
resourceType=&quot;Unspecified&quot; requireAccess=&quot;Script&quot; /&gt;
&lt;/handlers&gt;
&lt;/system.webServer&gt;
&lt;/configuration&gt;
</pre>
<p><strong>Step 7</strong>: Modified the RoR application to display some meaningful information. For that I opened the <strong>app\controller\test_controller.rb </strong>file and pasted the following code into it:</p>
<pre name="code" class="rails">
class TestController &lt; ApplicationController
def index
render :text=&gt;&quot;The index action&quot;
end
def about
render :text=&gt;&quot;The about action&quot;
end
end
</pre>
<p><strong>Step 8</strong>: Tested that my RoR application works by browsing to <strong>http://ruby/test/about</strong></p>
<p><a href="http://ruslany.net/wp-content/uploads/2008/08/rortestabout.png"><img style="border-width: 0px;" src="http://ruslany.net/wp-content/uploads/2008/08/rortestabout-thumb.png" alt="RoRTestAbout" width="504" height="291" border="0" /></a></p>
<p>Now let me show you why the handler mapping, that I have created in step 6, is problematic. If I try to request a static image file in my application I get this response:</p>
<p><a href="http://ruslany.net/wp-content/uploads/2008/08/rorroutingerror.png"><img style="border-width: 0px;" src="http://ruslany.net/wp-content/uploads/2008/08/rorroutingerror-thumb.png" alt="RoRRoutingError" width="504" height="324" border="0" /></a></p>
<p>This is because the &#8220;catch all&#8221; handler mapping for RoR causes the HTTP request for static file to be given to RoR. RoR tries to match the requested URL against all the routes defined for the application and since there is no route defined for static file the routing error is raised.</p>
<p>Now we will use <a title="URL Rewrite module download" href="http://www.iis.net/urlrewrite" target="_blank">URL rewrite module</a> to fix this problem. First, let&#8217;s take a look at <a title="RoR routing" href="http://wiki.rubyonrails.com/rails/pages/UnderstandingHowRequestsAreRouted" target="_blank">this article</a> that explains how RoR is supposed to be setup on Apache Web Server. According to this article, RoR relies on Apache mod_rewrite to always rewrite requested URL&#8217;s to <strong>dispatch.fcgi</strong> unless the requested file already exists on a file system. Let&#8217;s try to configure IIS URL rewrite module to do the same. To do that we will take these scary looking mod_rewrite rules:</p>
<pre name="code" class="css">
# Redirect all requests not available on the filesystem to Rails
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
</pre>
<p>and import them into IIS by using &#8220;Import Rules&#8221; feature of URL rewrite module:</p>
<p><a href="http://ruslany.net/wp-content/uploads/2008/08/rorimportrules.png"><img style="border-width: 0px;" src="http://ruslany.net/wp-content/uploads/2008/08/rorimportrules-thumb.png" alt="RoRImportRules" width="395" height="379" border="0" /></a></p>
<p>After saving the converted rules into the web.config file we will need to modify the handler mapping for RoR so that it does not use &#8220;*&#8221; mapping anymore:</p>
<pre name="code" class="xml">
&lt;handlers&gt;
&lt;add name=&quot;Ruby via FastCGI&quot;
path=&quot;dispatch.fcgi&quot;
verb=&quot;*&quot;
modules=&quot;FastCgiModule&quot;
scriptProcessor=&quot;C:\ruby\bin\ruby.exe|c:\inetpub\ruby\myapp\public\dispatch.fcgi development&quot;
resourceType=&quot;Unspecified&quot; requireAccess=&quot;Script&quot; /&gt;
&lt;/handlers&gt;
</pre>
<p>Now let&#8217;s test whether these modifications help to resolve the problem with serving static files. First we confirm that the RoR application still works. For that we browse to <strong>http://ruby/test/about</strong> again:</p>
<p><a href="http://ruslany.net/wp-content/uploads/2008/08/rortestabout1.png"><img style="border-width: 0px;" src="http://ruslany.net/wp-content/uploads/2008/08/rortestabout-thumb1.png" alt="RoRTestAbout" width="504" height="291" border="0" /></a></p>
<p>Then we check if requests for static files are handled correctly. For that we browse to <strong>http://ruby/myapp/public/images/rails.png</strong>:</p>
<p><a href="http://ruslany.net/wp-content/uploads/2008/08/rorstaticfile.png"><img style="border-width: 0px;" src="http://ruslany.net/wp-content/uploads/2008/08/rorstaticfile-thumb.png" alt="RoRStaticFile" width="504" height="331" border="0" /></a></p>
<p>As you can see the mod_rewrite rules that were imported from <a title="RoR Documentation" href="http://wiki.rubyonrails.com/rails/pages/UnderstandingHowRequestsAreRouted" target="_blank">RoR documentation</a> work quite well in IIS. The Ruby on Rails application is fully functional and we did not use any workarounds for that &#8211; all the necessary functionality is available in IIS 7.0.</p>
]]></content:encoded>
			<wfw:commentRss>http://ruslany.net/2008/08/ruby-on-rails-in-iis-70-with-url-rewriter/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
	</channel>
</rss>

