2015-02-23 88 views
70

在将我们团队的rails应用程序升级到4.2之后,如提到的release note,默认ip rails server绑定到更改为localhost0.0.0.0如何更改Rails 4.2开发服务器的默认绑定IP?

我们用Vagrant开发,并希望开发服务器可以直接从主机上的浏览器访问。

从现在开始每次都不用打字rails s -b 0.0.0.0,我想知道是否还有更优雅的解决方案,以便我们仍然可以像使用rails s那样简单来启动服务器。也许:

  • 配置文件rails s读取,我可以修改默认绑定IP(不使用-c
  • 端口转发与流浪者(尝试,但失败了,请参阅下面遇到的问题)
  • 猴子补丁机架,这改变了默认绑定ip

这背后的真正目标是我希望我们的团队能够顺利升级,避免人们由于缺少01而不得不不断重启他们的rails服务器的问题部分。

我试过流浪港口转发,但是当我访问主机上的localhost:3000时,仍然收到Connection Refused。我试过的两条配置线是:

config.vm.network "forwarded_port", guest: 3000, host: 3000 
config.vm.network "forwarded_port", guest: 3000, guest_ip: '127.0.0.1', host: 3000 

在官方文档中未找到任何相关说明。任何帮助将不胜感激。

+3

rails 5回答:https://stackoverflow.com/a/33852354/520567 – akostadinov 2016-07-28 07:23:59

回答

64

我在这里有同样的问题,我今天发现了一个更好的解决方案。只需将此代码附加到您的config/boot.rb,它应该可以与vagrant一​​起使用。

require 'rails/commands/server' 
module Rails 
    class Server 
    def default_options 
     super.merge(Host: '0.0.0.0', Port: 3000) 
    end 
    end 
end 

PS:它基于:this answer

+1

非常感谢您...... – 2015-05-15 22:24:38

+1

使用这种方法,您将失去[其他默认设置](https://github.com/rails/rails/blob/v4.2.5.1/railties/lib/rails/命令/ server.rb#L109)。请参阅下面Vanitas的[答案](http://stackoverflow.com/a/33249657/2464148)以获取替代方案。 – 2016-02-25 17:43:28

+0

我同意,看看Vanita的回答 – ringe 2016-09-16 05:55:38

34

您可以使用foreman运行与您的自定义命令的Procfile

# Procfile in Rails application root 
web:  bundle exec rails s -b 0.0.0.0 

现在开始使用Rails应用程序:

foreman start 

约工头的好处是,你可以添加其他应用程序到Procfile(如sidekiq,mailcatcher)。

工头坏事是你必须训练你的团队运行foreman start而不是rails s

+0

谢谢。如果事实证明是最好的解决方案,我会在几天后接受你的回答:) – 2015-02-26 05:32:37

+0

缩写的“领班s”也可以工作 - 可能会更容易从“rails s”过渡。 – 2015-06-15 08:15:07

5

如果你把config/boot.rb然后命令所有属性的默认选项耙和轨失败(例如:rake -Trails g model user)!所以,把这段bin/rails线require_relative '../config/boot'后的代码只对钢轨服务器执行的命令:

if ARGV.first == 's' || ARGV.first == 'server' 
    require 'rails/commands/server' 
    module Rails 
    class Server 
     def default_options 
     super.merge(Host: '0.0.0.0', Port: 3000) 
     end 
    end 
    end 
end 

bin/rails文件loks这样的:

#!/usr/bin/env ruby 
APP_PATH = File.expand_path('../../config/application', __FILE__) 
require_relative '../config/boot' 

# Set default host and port to rails server 
if ARGV.first == 's' || ARGV.first == 'server' 
    require 'rails/commands/server' 
    module Rails 
    class Server 
     def default_options 
     super.merge(Host: '0.0.0.0', Port: 3000) 
     end 
    end 
    end 
end 

require 'rails/commands' 
17

会见了同样的问题。找到了博客Make Rails 4.2 server listens to all interfaces

以下添加到配置/的boot.rb

require 'rails/commands/server' 

module Rails 
    class Server 
    alias :default_options_bk :default_options 
    def default_options 
     default_options_bk.merge!(Host: '0.0.0.0') 
    end 
    end 
end 
+1

这个答案还有另外一个好处,就是不会丢失[原始默认值](https://github.com/rails/rails/blob/v4.2.5.1/railties/lib/rails/commands/server.rb#L109 )(即端口3000!) – 2016-02-25 17:34:33

+0

此解决方案适用于我 – ringe 2016-09-16 05:54:45

0

这里有一个简单的解决方案,我使用。我已经喜欢/需要dotenvpuma-heroku,所以如果使用这些不适合你,那么这可能不适合你。

/config/puma.rb

plugin :heroku 

的Gemfile

gem 'dotenv-rails', groups: [:development, :test] 

.ENV

PORT=8080 

我现在可以同时启动和开发生产rails s