Run Rails under Yaws

ngocdaothanh edited this page Sep 13, 2010 · 8 revisions

With the FastCGI support added by the commit 2fa66b064d72205ef4886199a043bedb083bb78c, it is possible to run Rails under Yaws. Remember to read the document of yaws.conf and yaws_api (and yaws.tex) about FastCGI.

For each incoming request, Yaws will open a TCP connection to Rails and talk to it via FastCGI protocol. This means you must run Rails at a host:port to listen to Yaws via FastCGI protocol.

Yaws Side


Rails may respond slow to Yaws. To avoid timeout at Yaws side, edit yaws_cgi.erl to increase FCGI_READ_TIMEOUT_MSECS to a proper number, example:

-define(FCGI_READ_TIMEOUT_MSECS, 10000).


logdir   = log
ebin_dir = ebin

<server localhost>
  port     = 2999
  listen   =
  listen_backlog = 100
  appmods  = </, mod_rails>
  docroot = path/to/rails/application/public/directory
  fcgi_app_server = localhost:3000





out(Arg) ->
    case yaws_cgi:call_fcgi_responder(Arg) of
        R when is_list(R) ->
            case proplists:get_value(status, R) of
                404 -> {page, Arg#arg.server_path};  % Let Yaws try to serve static files
                _   -> R

        X -> X

Start it:

$ yaws

Rails Side

Create in the Rails application directory (it may be created by rake rails:update:generate_dispatchers):

require 'rubygems'
require 'rack'

require File.dirname(__FILE__) + '/config/environment', {:Port => 3000})

Start it:

$ rackup

Now browse to http://localhost:2999/ and you will see the Rails famous “Welcome aboard” page.