如何更改 Rails 3 中的默认 Rails 服务器? [英] How to change the default rails server in Rails 3?

查看:48
本文介绍了如何更改 Rails 3 中的默认 Rails 服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Rails 的新手,我想知道是否有一个选项可以更改默认的 Rails 服务器,即 webrick,为另一个服务器,例如puma"或thin".我知道可以使用rails server"命令指定要运行的服务器,但是我想在不指定服务器名称的情况下使用此命令,以便它可以运行默认的 rails 服务器.有没有办法将默认的 rails 服务器更改为配置文件或类似的内容?预先感谢您的帮助!

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!

推荐答案

我认为 rails 只是传递提供给机架的服务器选项.Rack 有以下逻辑来确定要运行的服务器:

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

第一种情况是将 :server 选项传递给 rails server 命令.二是确定默认.它看起来像:

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 和 Puma 应该会被自动拾取.后备方案是 Webrick.当然,其他 Web 服务器可以覆盖此行为,使它们成为链中的第一个.

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.

如果默认情况下您的网络服务器未被选中,您可以对 default 方法进行猴子补丁,使其按照您的意愿工作.当然,这可能会在机架的未来版本中中断.

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.

这篇关于如何更改 Rails 3 中的默认 Rails 服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆