如何更改 Rails 4.2开发服务器的默认绑定 ip?

在将我们团队的 Rail 应用程序升级到4.2之后,如 释放通知书所提到的,默认的 ip rails server绑定将从 0.0.0.0更改为 localhost

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

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

  • 一个配置文件 rails s读取其中我可以修改默认绑定 ip (不使用 -c)
  • 向前移动与流浪者(尝试但失败,见下面遇到的问题)
  • 猴子补丁到机架,这改变了默认的绑定 ip

这背后的真正目标是,我希望我们的团队之间的升级是顺利的,避免人们将不得不不断重新启动他们的轨道服务器,由于缺少 -b 0.0.0.0的部分。

我尝试了流浪端口转发,但仍然得到 Connection Refused当我访问主机上的 localhost:3000。我尝试的两条配置线是:

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

在官方文件中没有找到任何相关的说明。如果有任何帮助,我们将不胜感激。

65144 次浏览

You can use foreman to run a Procfile with your custom commands:

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

Now start your Rails application with:

foreman start

The good thing about foreman is that you can add other applications to the Procfile (like sidekiq, mailcatcher).

The bad thing about foreman is that you have to train your team to run foreman start instead of rails s.

I'm having the same issue here and I found today a better solution. Just append this code to your config/boot.rb and it should work with 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: Its based on: this answer

If you put the default options on config/boot.rb then all command attributes for rake and rails fails (example: rake -T or rails g model user)! So, append this to bin/rails after line require_relative '../config/boot' and the code is executed only for the rails server command:

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

The bin/rails file loks like this:

#!/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'

Met the same problem. Found the blog Make Rails 4.2 server listens to all interfaces.

Add the following to config/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

Here's a simpler solution that I'm using. I already like/need dotenv and puma-heroku, so if using those doesn't work for you then this might not be for you.

/config/puma.rb

plugin :heroku

Gemfile

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

.env

PORT=8080

Now I can start both dev and production with rails s.

Switch to Puma and specify port in config/puma.rb, e.g.:

port        ENV.fetch("PORT") { 3000 }

Apparently it will bind to 0.0.0.0 for the specified port: https://github.com/puma/puma/issues/896

For Rails 5.1.7 with Puma 3.12.1 the selected answer does not work, but I accomplished it by adding the following to my config/puma.rb file:

set_default_host '0.0.0.0' # Note: Must come BEFORE defining the port


port ENV.fetch('PORT') { 3000 }

I determined this by inspecting the dsl file. It uses instance_eval on that file, so there are probably other ways to do it, but this seemed the most reasonable to me.

If you use docker or another tool to manage the environment variables, you can set the HOST environment variable to the IP you need to bind.

Example: HOST=0.0.0.0

Add it to docker.env file if you use Docker or .env if you use foreman.

For Rails 5 with Puma the selected answer does not work. You may get such error: cannot load such file -- rails/commands/server

For proper solution add following to config/puma.rb:

bind 'tcp://0.0.0.0:3000'