Ruby on Rails in IIS 7.0 with URL Rewriter
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’s that look similar to this: http://mysite.com/home/about. In order for RoR to be invoked for this kind of URL’s it is necessary to create a “catch all” handler mapping in IIS (that is a handler mapping with path attribute set to “*“). 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.
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 – this article 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.
First let’s setup RoR. To set it up I used instructions from these two great articles:
- Mike Volodarsky’s 10 steps to get Ruby on Rails running on Windows with IIS FastCGI
- Ruby On Rails wiki article How To Configure IIS7
Both articles are a bit outdated, so I ended up with performing the following steps:
Step 1: Installed latest version of Ruby by using One-Click Installer. At the time of writing this post the latest stable version was 1.8.6-26.
Step 2: Installed Rails by running gem installer from command line window:
gem intsall rails --include-dependencies
Step 3: Installed RoR extensions for IIS. After installation completed I checked that everything was setup successfully by typing the following in the command line window:
C:\>irb irb(main):001:0> require 'fcgi' => true irb(main):002:0> quit
Step 4: Created a web site in IIS with physical path C:\inetpub\ruby and with host name “ruby”. Also modified the %WINDIR%\system32\drivers\etc\hosts file to include this line:
127.0.0.1 ruby
Note that if you want to have RoR application in the “Default Web Site” in IIS you do not need to do this step.
Step 5: Created a test RoR application in a root folder of a web site:
cd c:\inetpub\ruby rails myapp cd myapp ruby script\generate controller test index
Step 6: Created a temporary FastCGI handler mapping for RoR:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="Ruby via FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\ruby\bin\ruby.exe|c:\inetpub\ruby\myapp\public\dispatch.fcgi development" resourceType="Unspecified" requireAccess="Script" /> </handlers> </system.webServer> </configuration>
Step 7: Modified the RoR application to display some meaningful information. For that I opened the app\controller\test_controller.rb file and pasted the following code into it:
class TestController < ApplicationController def index render :text=>"The index action" end def about render :text=>"The about action" end end
Step 8: Tested that my RoR application works by browsing to http://ruby/test/about
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:
This is because the “catch all” 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.
Now we will use URL rewrite module to fix this problem. First, let’s take a look at this article 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’s to dispatch.fcgi unless the requested file already exists on a file system. Let’s try to configure IIS URL rewrite module to do the same. To do that we will take these scary looking mod_rewrite rules:
# 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]
and import them into IIS by using “Import Rules” feature of URL rewrite module:
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 “*” mapping anymore:
<handlers> <add name="Ruby via FastCGI" path="dispatch.fcgi" verb="*" modules="FastCgiModule" scriptProcessor="C:\ruby\bin\ruby.exe|c:\inetpub\ruby\myapp\public\dispatch.fcgi development" resourceType="Unspecified" requireAccess="Script" /> </handlers>
Now let’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 http://ruby/test/about again:
Then we check if requests for static files are handled correctly. For that we browse to http://ruby/myapp/public/images/rails.png:
As you can see the mod_rewrite rules that were imported from RoR documentation work quite well in IIS. The Ruby on Rails application is fully functional and we did not use any workarounds for that – all the necessary functionality is available in IIS 7.0.
8,189 views
ruslany on August 7th 2008 in FastCGI, URLRewrite


step 3, did you follow the full instructions or just install the “RoR addin for IIS”? As in doing the full FastCGI module, the problem i have is that those instructions are for IIS 6 and win2k3, does it indeed need to be configured the same way, or will URL Rewriter replace the ISAPI rewrite module?
In Step 7, how do you add a handler to IIS 7 this way through XML? I am not that familiar with IIS 7.
Thanks for the great article, and getting RoR on IIS 7.
In step 3: I did not follow the instructions for addin. I just installed the addin and then checked that it worked by following the instructions in step 4) of Mike Volodarsky’s blog post.
In step 7: to add the handler mapping, I placed the xml section inside of the web.config file located in the root folder of my web site. I’ve updated the post to show where this xml should be located inside of the web.config file.
Does this work for the x64 bit version of Windows 2008?
Awajan,
I have not tried this on x64 bit. I could not find the 64bit version of Ruby, but 32bit version should work in theory, if you configure IIS application pool to enable 32-bit applications (enable32BitAppOnWin64)
It looks like the URL mentioned in step #3 for installing Ruby Extensions for IIS is down. So, until it comes back online, here is the copy of the RubyForIIS that I have downloaded from there last summer.
[...] Ruby on Rails in IIS 7.0 with URL Rewriter [...]
I’m having a problem in Step 6 “Created a temporary FastCGI handler mapping for RoR”
My problem is that i don’t have a web.config file, so where do i put this code ?
Maybe it’s a dumb question but, does the web.config file creates automatically when i create a web site ? Because my website doesn’t have any web.config file.
Alex, when you create a new web site the web.config file is not created. If you do not have web.config file then create it manually and paste the provided XML into it.
thanks for replying so fast
i created my web.config file, but i’m getting an error in step 8 when go to “http://ruby/test/about” it does’nt show me the page, it throws me the following error:
HTTP Error 500.0 – Internal Server Error
c:\ruby\bin\ruby.exe – The FastCGI process exited unexpectedly
Module FastCgiModule
Notification ExecuteRequestHandler
Handler Ruby via FastCGI
Error Code 0×00000001
Requested URL http://ruby:80/test/about
Physical Path C:\Inetpub\ruby\test\about
Logon Method Anonymous
Logon User Anonymous
BTW in step 5 i created my app using “rails -d mysql myapp” but i’ve already create a database and user myapp in mysql anyway here is my “database.yml” file if that helps:
development:
adapter: mysql
encoding: utf8
database: myapp
pool: 5
username: myapp
password: myapp
host: localhost
i check the article on “10 steps to get Ruby on Rails running on Windows with IIS FastCGI” and many people had the same problem due to folder permissions, i’ve already assigned all this but had no luck, any suggestion ?
Thanks in advance..
Alex, it’s been some time since I worked with RoR and I do not remember seeing this error when I install it last time. Let me try to re-install it again and see if I can repro the same problem.
I was interested in implementing RoR on IIS, but the extension for IIS link appears to still be broken. Is this extension still needed? Is there an alternate download?
Thanks!
smepps, you can download it from here.
Hmm, I’m getting a 404 – Not Found
Try again – it should work now.
Thank you.
Hi, good article, thanx
i have done this:
ruby on rails on win2k8 IIS7 sql2k8 with activerecord-sqlserver-adapter-1.0.0.9250 radiant cms application imported URL Rewrites and FastCgi Handler like this:
All work ok, but [put] data Redirected to http://www.gproject.ch/admin/pages/11/edit
raise a HTTP 500 – Internal server error. Data are stored in sql and if I refresh, I get the correct site back. What do I wrong ???? Thanx for any suggestion
Looks like the actual xml code was removed from the comment. Can you just send it to me via the contact form?
In Step 6
1. Where do you save this file? When I saved it under the app
C:\inetpub\ruby\web.config – It said overriding is not allowed
2. Then I edited application.config under Inetsrv\config
But then I am not sure \ what the segment “|c:\inetpub\ruby\myapp\public\dispatch.fcgi development”
does. If I add it is bombing out.
Thanks for your help
[...] that there are very few articles on this. A few I found were outdated. Then I landed on Ruslan's post. It got me to a point but then I was stuck. So I walked to Ruslan's office and we discussed this [...]
I’m a little baffled here. I’ve followed the instructions to the letter, and confirmed that the ruby installation is correct. I’ve installed the FastCgi module and put in all the code, but I receive an error when I try to access the page. The error is 500.0 error and when I trace the error with IIS it tells me it’s a FastCgi error.
here is the error message from the iis trace file. (actaully it’s the 3 lines, 47, 48, and 49, with 49 being the descriptive error)
47. +FASTCGI_UNEXPECTED_EXIT
Warning
48. -SET_RESPONSE_ERROR_DESCRIPTION
ErrorDescription C:\ruby\bin\ruby.exe – The FastCGI process exited unexpectedly
Warning
49. -MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName FastCgiModule
Notification 128
HttpStatus 500
HttpReason Internal Server Error
HttpSubStatus 0
ErrorCode 1
ConfigExceptionInfo
Notification EXECUTE_REQUEST_HANDLER
ErrorCode Incorrect function. (0×1)
any help would be great.
Share, it’s been a while since I wrote this article and I think there have been some changes in the installation process. Please try following the steps described here: http://blogs.msdn.com/dgorti/archive/2009/06/17/ruby-on-rails-with-iis-7-reloaded.aspx
Thanks so much ruslany. I appreciate the help.