Skip to content

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

FCGI_READ_TIMEOUT_MSECS

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

yaws.conf

logdir   = log
ebin_dir = ebin

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

mod_rails.erl

-module(mod_rails).

-compile(export_all).

-include_lib("yaws/include/yaws_api.hrl").

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
            end;

        X -> X
    end.

Start it:

$ yaws

Rails Side

Create config.ru 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'
Rack::Handler::FastCGI.run(ActionController::Dispatcher.new, {:Port => 3000})

Start it:

$ rackup

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

Clone this wiki locally