如何配置Rails 5.2.1服务器在所有接口上侦听?

时间:2018-10-12 19:19:03

标签: ruby-on-rails ruby ruby-on-rails-5.2

我对RoR和学习还是相当陌生,所以如果我有后续问题,请耐心等待。我们的Rails(v 5.0.2)应用已配置为在config/boot.rb中使用此配置监听所有接口:

require 'rails/commands/server'
module Rails
  class Server
    def default_options
      # make rails listen on all interfaces (accept connections from any ip)
      super.merge(Host: '0.0.0.0', Port: 1234)
    end
  end
end

我最近将Rails从5.0.2升级到5.2.1,当我运行rails s时,我得到了:

/Users/aum/rails_app/config/boot.rb:5:in `require': cannot load such file -- rails/commands/server (LoadError)
    from /Users/aum/rails_app/config/boot.rb:5:in `<top (required)>'
    from bin/rails:8:in `require_relative'
    from bin/rails:8:in `<main>'

所以我将'require'更新为rails/commands/server/server_command,现在我得到了

rails s
/Users/aum/.rvm/gems/ruby-2.3.3@rails_app/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:110:in `<module:Command>': uninitialized constant Rails::Command::Base (NameError)
Did you mean?  Base64
  from /Users/aum/.rvm/gems/ruby-2.3.3@rails_app/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:109:in `<module:Rails>'
  from /Users/aum/.rvm/gems/ruby-2.3.3@rails_app/gems/railties-5.2.1/lib/rails/commands/server/server_command.rb:11:in `<top (required)>'
  from /Users/aum/rails_app/config/boot.rb:5:in `require'
  from /Users/aum/rails_app/config/boot.rb:5:in `<top (required)>'
  from bin/rails:8:in `require_relative'
  from bin/rails:8:in `<main>'

我不确定在Rails 5.2.1中如何指定主机和端口,因为default_options的定义在这里也已更改:https://github.com/nokedajunky/cucumber2

注意:我可以使用rails s -b 0.0.0.0 -p 1234启动服务器,但这不是我要尝试的操作。

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

假设您正在使用puma(这是Rails 5的默认HTTP服务器),则绑定选项在config/puma.rb中定义:

bind 'tcp://0.0.0.0:3000'