2015-02-23 87 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/railsrequire_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