Search code examples
ruby-on-railsrubynginxrack

Single server instance for multiple Ruby (rails/sinatra) applications


In PHP I could have single php-fpm instance to serve multiple sites. The nginx config I had was looking like so:

upstream backend 
{
  server 127.0.0.1:9000;
}

# site 1
server 
{
  server_name www.site1.com;
  root /var/www/site1;

  location ~ \.php$ 
  {
    include fastcgi_params;
    fastcgi_pass backend;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}


# site 2
server 
{
  server_name www.site2.com;
  root /var/www/site2;

  location ~ \.php$ 
  {
    include fastcgi_params;
    fastcgi_pass backend;     # still the same backend
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

What I see in Ruby world is that for each application a separate independent server is started which listens on a specific port and can can handle requests only to this application. So I'm wondering, is it possible to have the same approach with Ruby applications as I could have with PHP above.

I understand that this could be not very good idea for high-traffic sites, but I'm actually dealing with quite low-traffic sites and my VPS has limited RAM.


Solution

  • I'm afraid not. Rails' runtime model is a bit different from php. With php, everything is loaded and executed, then dropped again. Nothing shared between each request. This means that the same process can serve requests from completely different applications. In a Rails (or Sinatra) setup, there is an application process running, which responds to requests. This process is comparatively heavy to boot up, so it isn't feasible to setup and tear down for each request. The result is that you need to accept that these processes hang on to system resources even when they aren't working.