Search code examples
ruby-on-railsruby-on-rails-3webserver

How to change the default rails server in Rails 3?


I'm new to Rails and I'm wondering if there is an option to change the default rails server, i.e., webrick, for another one such as 'puma' or 'thin'. I know it is possible to specify which server to run with 'rails server' command, however I would like to use this command without specify the name of the server so it can run the default rails server. Is there a way to change the default rails server into a configuration file or something like this? Thanks in advance for your help!


Solution

  • I think rails simply passes on the server option provided to rack. Rack has the following logic to determine what server to run:

    https://github.com/rack/rack/blob/master/lib/rack/server.rb#L271-L273

    def server
      @_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
    end
    

    The first case is when a :server option was passed to the rails server command. The second is to determine the default. It looks like:

    https://github.com/rack/rack/blob/master/lib/rack/handler.rb#L46-L59

    def self.default(options = {})
      # Guess.
      if ENV.include?("PHP_FCGI_CHILDREN")
        # We already speak FastCGI
        options.delete :File
        options.delete :Port
    
        Rack::Handler::FastCGI
      elsif ENV.include?("REQUEST_METHOD")
        Rack::Handler::CGI
      else
        pick ['thin', 'puma', 'webrick']
      end
    end
    

    Thin and Puma should be automatically picked up. The fallback is Webrick. Of course other web servers could override this behavior to make them the first in the chain.

    If your Webserver is not picked up by default you could monkey-patch the default method to work like you want it. Of course this could break in future versions of rack.