简体   繁体   English

如何在Rails 3中更改默认的rails服务器?

[英]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, ie, webrick, for another one such as 'puma' or 'thin'. 我是Rails的新手,我想知道是否有一个选项来更改默认的rails服务器,即webrick,用于另一个例如'puma'或'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. 我知道可以使用'rails server'命令指定运行哪个服务器,但是我想使用此命令而不指定服务器的名称,以便它可以运行默认的rails服务器。 Is there a way to change the default rails server into a configuration file or something like this? 有没有办法将默认的rails服务器更改为配置文件或类似的东西? Thanks in advance for your help! 在此先感谢您的帮助!

Based on James Hebden 's answer: 基于James Hebden的回答:

Add Puma to gemfile Puma添加到gemfile

# Gemfile
gem 'puma'

Bundle install it 捆绑安装它

bundle

Make it default, paste this code into script/rails above require 'rails/commands' : 将其设为默认值,将此代码粘贴到require 'rails/commands' script/rails上:

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)

So script/rails (in Rails 3.2.12 ) will look like: 所以script/rails (在Rails 3.2.12 )将如下所示:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
require 'rails/commands'

Run server 运行服务器

rails s
=> Booting Puma

Rack (the interface between rails and a web server) has handlers for the default WEBrick, and also for Thin. Rack(rails和web服务器之间的接口)具有默认WEBrick和Thin的处理程序。 If you place the following in your Gemfile in the root of your rails project 如果将以下内容放在rails项目根目录中的Gemfile

gem 'thin'

rails server will automatically use Thin. rails服务器会自动使用Thin。 This has been the case since 3.2rc2. 自3.2rc2以来就是如此。

This unfortunately only applies to Thin, as Rack does not have built-in support for Unicorn, and others. 遗憾的是,这仅适用于Thin,因为Rack没有内置支持Unicorn等。

For servers that have Rack handlers (again, sadly Unicorn does not), you can do a bit of a hack to get rails server to use them. 对于拥有Rack处理程序的服务器(再次,可悲的是Unicorn没有),你可以做一些破解让rails服务器使用它们。 In your scripts/rails file in the root of your rails project, you can add the below just above `require 'rails/commands' 在rails项目根目录的scripts / rails文件中,您可以在`require'trail / commands'上面添加以下内容

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::<name of handler class>

This essentially resets the handler for WEBrick to point to the handler for the server you would like to use. 这实质上将WEBrick的处理程序重置为指向您要使用的服务器的处理程序。

To get an understanding of the supported Rack handlers, take a look at the comments in the source: https://github.com/rkh/rack/blob/master/lib/rack/handler.rb 要了解支持的Rack处理程序,请查看源代码中的注释: https//github.com/rkh/rack/blob/master/lib/rack/handler.rb

I think rails simply passes on the server option provided to rack. 我认为rails只是传递给提供给机架的服务器选项。 Rack has the following logic to determine what server to run: Rack具有以下逻辑来确定要运行的服务器:

https://github.com/rack/rack/blob/master/lib/rack/server.rb#L271-L273 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. 第一种情况是将:server选项传递给rails server命令。 The second is to determine the default. 第二是确定默认值。 It looks like: 看起来像:

https://github.com/rack/rack/blob/master/lib/rack/handler.rb#L46-L59 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. 应该自动拾取Thin和Puma。 The fallback is Webrick. 后退是Webrick。 Of course other web servers could override this behavior to make them the first in the chain. 当然,其他Web服务器可以覆盖此行为,使其成为链中的第一个。

If your Webserver is not picked up by default you could monkey-patch the default method to work like you want it. 如果默认情况下没有选择您的Web服务器,您可以修改default方法以使其工作。 Of course this could break in future versions of rack. 当然,这可能会在未来版本的机架中出现问题。

Rack will now look at a RACK_HANDLER environment variable file to see if you've specified a default rack handler. Rack现在将查看RACK_HANDLER环境变量文件,以查看您是否指定了默认的机架处理程序。 You can add a line like this to your .env file to set the default if you're using dotenv, or specify the assignment from the command line. 如果您使用的是dotenv,则可以将这样的行添加到.env文件中以设置默认值,或者从命令行指定赋值。

`RACK_HANDLER=webrick`

This should work as of this pull request: 这应该适用于此拉取请求:

https://github.com/rack/rack/pull/590 https://github.com/rack/rack/pull/590

If you want unicorn/thin/etc, just add the gem to your gemfile 如果你想要unicorn / thin / etc,只需将gem添加到你的gemfile中

ie gem 'unicorn' , gem 'thin' , etc. then run bundle install at the command line. gem 'unicorn'gem 'thin'等,然后在命令行运行bundle install

As far as I can tell, adding either of these gems runs the appropriate server via rails server 据我所知,添加这些宝石中的任何一个都可以通过rails server运行相应的rails server

UPDATE UPDATE

Apparently this only works for Thin or Puma. 显然这仅适用于Thin或Puma。

I wouldn't get hung up on specifically using the rails server command. 我不会专门使用rails server命令。 Just install whichever gem you want and alias the command (eg rails s Puma ) to something simple like rs . 只需安装你想要的任何宝石,并将命令(例如rails s Puma )别名为rs简单东西。

If you have thin in your Gemfile, you need to do this: 如果你的Gemfile中有 ,你需要这样做:

require 'rack/handler'
Rack::Handler::Thin = Rack::Handler.get(:puma)

如果你使用bash run: export RACK_HANDLER=webrick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM