Running Rails applications: Choosing a server

Once you develop a web application in Rails, and works great in the embedded web server rails comes with, or mongrel, you may ask yourself how to run the application for production.
It may seem it's easy to decide, and in fact, all the solutions will be around Apache2 or lighttpd + FastCGI.

Actually, as you may have noticed, the performance difference between Apache2 and lighttpd is not the problem. They both work fine (and if you don't need something specific only available for one of the web servers, you can choose the one you prefer). The biggest problem is the FastCGI module.

The FastCGI module for Apache (libapache2-mod-fastcgi in Debian systems) does not perform good, even changing the default configuration (/etc/apache2/mods-available/fastcgi.conf)

By default, the module runs in dynamic mode, which is, you give a minimum and maximum of FastCGI processes running, and these are started or killed automatically when it's needed.

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  ...
  FastCgiConfig -minProcesses 2 -maxProcesses 6
  ...
</IfModule>

It can also be configured in static mode, where the number of FastCGI processes specified are started when Apache starts, and remain running. You can adjust the idle timeout parameter to the needs of your application. In the example, it gives a 60 second limit to process the query.

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  ...
  FastCgiServer -idle-timeout 60 -processes 4
  ...
</IfModule>

I experienced better performance using FastCGI in static mode for Rails, but it is not good enough. The dispatch.fcgi processes freeze from time to time and the application is either slow or crashes.

The solution I am currently using is Apache2 + libapache2-mod-fcgid. mod-fcgid is a module for Apache2, compatible with FastCGI that I found to perform much better than the other solutions above. Even with the default configuration is much better than the FastCGI module for Rails.

There are some small things you have to change to use fcgid, though. First of all, you will have to change the handler in the .htaccess file in the public/ directory of your Rails application.
You will have to search for the line:
    AddHandler fastcgi-script .fcgi
replacing it with:
    AddHandler fcgid-script .fcgi

If you use HTTP Authentication, you will notice that the header is not passed through the cgi to the rails application. You will have to use RewriteRules to pass it. Again, in .htaccess, search for the line:
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
and replace it with:
    RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]

Now you can access the Authorization header the usual way:

if request.env.has_key? 'REDIRECT_X_HTTP_AUTHORIZATION'
    authdata =
request.env['REDIRECT_X_HTTP_AUTHORIZATION'].to_s.split
end





 


Trackback

Trackback URL for this entry:
http://www.gra2.com/trackback.php/running-rails-choosing-a-server

Here's what others have to say about 'Running Rails applications: Choosing a server':

Benchmarking Rails enabled Web Servers from newton.gra2.com
Following the article Running Rails applications: Choosing a server where I compared the different combinations server/dispatcher available for Rails, I have done a small benchmark to see what server actually performs better. The servers I have test... [read more]
Tracked on Friday, March 09 2007 @ 11:17 PM CET

Comments

Post a comment

nice

Anonymous on Sunday, July 04 2010 @ 04:19 AM CEST Reply | #

HsE6xv I almost accidentally visited to this site, but stayed here for a long time. Stayed because everything was very interesting. Surely will share with all my friends!...

Buy cheap OEM software on Wednesday, September 28 2011 @ 04:22 PM CEST Reply | #

OqEBXw I would add something else, of course, but in fact almost everything is mentioned!...

OEM software download on Thursday, September 29 2011 @ 05:11 AM CEST Reply | #

Search



About

newton.gra2.com is a blog about technology, opinion and random thoughts written by Daniel Alvarez, a computer engineer currently living in Zurich, Switzerland.

Topics

News (20/0)
Manuals (24/0)
Security (7/0)
Music (3/0)
Weeklog (1/0)
Personal (34/0)
Photos (3/0)
Opinion (14/0)
Windows (5/0)

Blogroll

Pros i contres (Jordi)
Entrepa de fusta (Oriol)
Spaghetti Code (Isaac)
Made in net (Eric)
Nogare (Juan)
Blog de Isaac Jimenez
Web d'en Jaume Benet
Montcada Wireless (Fran)
Blog d'en Ricard Forniol
Angela Fabregues
in.solit.us

Libertad Digital
FOX News
The Wall Street Journal
The Washington Times
The Jerusalem Post

Michelle Malkin
Eurabian News
Nihil Obstat
Barcepundit
Expose the left
Davids Medienkritik
Johan Norberg
Ayaan Hirsi Ali

User Functions

:

:


Lost your password?

Latest posts

Stories

No new stories

Comments last 2 days


Trackbacks last 2 days

No new trackback comments

Links last 2 weeks

No recent new links