Ruby on Rails in IIS 7.0 with URL Rewriter

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 Rails in IIS 7 provided by Helicon Zoo Web Framework Repository.

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:

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

RoRTestAbout

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:

RoRRoutingError

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:

RoRImportRules

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:

RoRTestAbout

Then we check if requests for static files are handled correctly. For that we browse to http://ruby/myapp/public/images/rails.png:

RoRStaticFile

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.

52,350 views

ruslany on August 7th 2008 in FastCGI, URLRewrite

PoorFairAverageGoodExcellent (6 votes, average: 4.83 out of 5)

31 Responses to “Ruby on Rails in IIS 7.0 with URL Rewriter”

  1. Gravatar ImageLucas responded on 27 Aug 2008 at 12:50 pm #

    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.

  2. Gravatar Imageruslany responded on 27 Aug 2008 at 7:41 pm #

    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.

  3. Gravatar ImageAwajan responded on 03 Nov 2008 at 10:54 am #

    Does this work for the x64 bit version of Windows 2008?

  4. Gravatar Imageruslany responded on 03 Nov 2008 at 11:38 am #

    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)

  5. Gravatar Imageruslany responded on 10 Jan 2009 at 12:50 am #

    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.

  6. Gravatar Imagehotgazpacho » Blog Archive » Rails 2.3.0 + IIS7 + FastCGI = Rails on Windows FTW! - yes, it is an oxymoron… responded on 07 Feb 2009 at 9:01 pm #

    [...] Ruby on Rails in IIS 7.0 with URL Rewriter [...]

  7. Gravatar ImageAlex responded on 27 Feb 2009 at 5:50 am #

    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.

  8. Gravatar Imageruslany responded on 27 Feb 2009 at 9:54 am #

    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.

  9. Gravatar ImageAlex responded on 27 Feb 2009 at 4:16 pm #

    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

  10. Gravatar ImageAlex responded on 02 Mar 2009 at 7:20 am #

    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..

  11. Gravatar Imageruslany responded on 02 Mar 2009 at 10:09 am #

    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.

  12. Gravatar Imagesmepps responded on 17 Mar 2009 at 8:28 am #

    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!

  13. Gravatar Imageruslany responded on 17 Mar 2009 at 8:34 am #

    smepps, you can download it from here.

  14. Gravatar Imagesmepps responded on 17 Mar 2009 at 9:37 am #

    Hmm, I’m getting a 404 – Not Found

  15. Gravatar Imageruslany responded on 17 Mar 2009 at 9:55 am #

    Try again – it should work now.

  16. Gravatar Imagesmepps responded on 17 Mar 2009 at 10:02 am #

    Thank you.

  17. Gravatar Imagerole responded on 03 Jun 2009 at 3:39 am #

    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

  18. Gravatar Imageruslany responded on 03 Jun 2009 at 11:16 pm #

    Looks like the actual xml code was removed from the comment. Can you just send it to me via the contact form?

  19. Gravatar Imagegorti responded on 16 Jun 2009 at 12:34 am #

    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

  20. Gravatar ImageDurgaprasad Gorti's WebLog : Ruby On Rails with IIS 7 - Reloaded responded on 19 Jun 2009 at 12:52 am #

    [...] 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 [...]

  21. Gravatar ImageShare responded on 06 Aug 2009 at 8:02 am #

    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.

  22. Gravatar Imageruslany responded on 07 Aug 2009 at 10:45 am #

    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

  23. Gravatar ImageShare responded on 12 Aug 2009 at 5:24 am #

    Thanks so much ruslany. I appreciate the help.

  24. Gravatar ImageSohan responded on 04 Mar 2010 at 10:19 pm #

    Your post is linked at http://www.DrinkRails.com blog!

  25. Gravatar Imagesim responded on 21 Mar 2010 at 11:45 pm #

    I still can’t understand step 6, where should I put the configure file web.config ?

  26. Gravatar Imageruslany responded on 25 Mar 2010 at 2:05 pm #

    @sim: the web.config file should be placed in the root directory of the web site.

  27. Gravatar ImageMark responded on 17 Mar 2011 at 9:38 am #

    That’s what I get:
    irb(main):001:0> require ‘fcgi’
    LoadError: no such file to load — fcgi
    from :29:in `require’
    from :29:in `require’
    from (irb):1
    from d:/Ruby192/bin/irb:12:in `’

    Any help?

  28. Gravatar ImageKlaus responded on 02 Jun 2011 at 9:28 am #

    Hallo,

    good instruction – all works fine until 128 requests have been received. Then the ruby application is stopping an restarting. That takes too much time. Do you know where I can change that 128-requests-limit?

    Please help.

    Best regards
    Klaus

  29. Gravatar Imageruslany responded on 06 Jun 2011 at 2:54 pm #

    Hi Klaus, I do not know how to change that limit.

    I would recommend to take a look at http://www.helicontech.com/zoo/, which also has RoR support.

  30. Gravatar Imageweatchley responded on 29 Jul 2011 at 10:25 am #

    ruslany,

    I have seen a lot of posts of people recomending Helicon Zoo.
    I have tried it on my Win 2008/IIS 7.5 server.
    The default site that it installs works just fine in a sub directory/app of the site, but when I replace the code with my rails app, I have routing issues.

    I have tried to install at the root of the server, this works for the ruby/rails code, but all static content is lost. It also keeps the page title as BlankRailsProject.

    I have searched the net and can find no examples for using the Helicon Zoo product. Do you have any examples or know of any that could help?

    Thank you in advance for your help

  31. Gravatar Imageyaroslav responded on 29 Jul 2011 at 1:54 pm #

    weatchley, please send your message to support@helicontech.com our support service will assist you.

Trackback URI | Comments RSS

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

XML Markup: If You want to add XML code to the comment please XML encode it first, otherwise the code will not show up.

Recently Published Articles