The "Tech. Arch."

Architecting Forward ::>

Riding out of the rail yard

For the last several months, I have been feeling the pain of shared hosting. My initial Ruby on Rails installation was stuck in the rail yard! Although I had cross-referenced my setup against many rails sites and blogs, I could not seem to get a ruby train out of my rail yard!!! Executing Ruby and CGI-based Ruby programs was fine. But no Rails! You see, my host – even with a business plan – does not let you touch your Apache httpd.conf. My only option is to perform limited configuration using .htaccess files. Moreover, the shared host settings are tucked away from view. Today, after weeks of various research and experiments, I discovered that handlers for CGI and FastCGI seemed to be defined at a high-level in the shared hosting setup. So overriding them seem to wreak havoc and interfere with Rails.

Here is what I had to do:

1. Typically a Rails rake-generated .htaccess sets the CGI and FastCGI handlers like so:

AddHandler fastcgi-script .fcgi
AddHandler cgi-script .cgi

However I discovered that for my shared host setup, if I comment the handlers out (or remove them altogether), then the dispatch.fcgi script acting as a front-controller for Rails starts working correctly, i.e. all Apache HTTP variables get passed correctly.

2. The .htaccess also includes Apache “voodoo” to rewrite your urls and specify that you want urls for your Rails app to be processed by the Ruby on Rails Fast CGI dispatcher. Like so:

RewriteCond %{REQUEST_FILENAME} !-F
RewriteRule ^(.*)$ /dispatch.fcgi [QSA,L][/Code]
In plain English, this means: "If a url represents a resource which does not exist as a physical file, then have my Fast CGI dispatcher process the request".

Unlike ASP.NET, JSP, PHP or other dynamic web page based apps, a Rails application exposes its features through a multi-level URL domain. Example:

[html]http://myapp.myhost.com/account/register
http://myapp.myhost.com/account/updateprofile
http://myapp.myhost.com/blog/post[/html]

In the classic ASP.NET world, specific .aspx pages are created to expose this functionality. Btw Microsoft is now providing a similar approach with the new MVC framework (see <a title="http://www.asp.net/mvc/" href="http://www.asp.net/mvc/">http://www.asp.net/mvc/</a>).
The shared hosting twist was that a very precise location must be provided for the Fast CGI, i.e. for a subdomain mapped to the public folder in Rails, the root (<strong>slash</strong>) must be included in front of the name!!! Obvious once you discover that, but oblivious when all examples anywhere never show that!

[code]RewriteRule ^(.*)$ /dispatch.fcgi [QSA,L]

3. The final step was to remove the dispatch.cgi also generated by rake since I am only using Fast CGI. It seems that keeping it was also interfering.
Phew!!! Now navigating to any Rails url works!!!

Here are a couple useful links worth adding to your del.icio.us collection:

  • .htaccess

http://httpd.apache.org/docs/trunk/howto/htaccess.html
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

  • Fast CGI

http://www.fastcgi.com/devkit/doc/fastcgi-whitepaper/fastcgi.htm

April 5th, 2008 Posted by | Ruby | no comments

No Comments

No comments yet.

Sorry, the comment form is closed at this time.