After upgrading our team's rails application to 4.2, as the release note mentioned, the default ip rails server
binds to is changed to localhost
from 0.0.0.0
.
在将我们团队的rails应用程序升级到4.2之后,正如发布说明所提到的,默认的ip rails服务器绑定到的是由0.0.0.0.0改为localhost。
We develop with Vagrant, and want the development server to be accessible directly from browser on the host machine.
我们使用Vagrant开发,并希望开发服务器可以直接从主机上的浏览器访问。
Instead of typing rails s -b 0.0.0.0
every time from now on, I wonder if there's any more elegant solution, so that we can still use sth as simple as rails s
to start the server. Perhaps:
从现在开始,我不再每次都输入rails s -b 0.0.0.0.0,我想知道是否还有更好的解决方案,以便我们仍然可以使用rails s这样简单的东西来启动服务器。可能:
- a config file
rails s
reads where I can modify the default binding ip (without using-c
) - 配置文件rails读取我可以修改默认绑定ip的地方(不使用-c)
- port forward with vagrant (tried but failed, see problem encountered below)
- 港口前锋与流浪汉(尝试但失败,见下面遇到的问题)
- a monkey patch to rack, that changes the default binding ip
- 一个猴子补丁到机架,改变默认的绑定ip
The real goal behind this is that I want the upgrade to be smooth among our team, avoiding the glitch that people will have to constantly restarting their rails server due to the missing -b 0.0.0.0
part.
这背后的真正目的是,我希望升级能够在我们的团队中顺利进行,避免由于缺少-b 0.0.0.0部分,人们需要不断重新启动rails服务器。
I tried vagrant port forwarding, but still get Connection Refused
when I visit localhost:3000
on the host machine. The two configuration lines I tried was:
我尝试了漫游端口转发,但是当我访问本地主机: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
Didn't find any relevant instructions in the official docs. Any help will be appreciated.
没有在官方文档中找到相关的说明。如有任何帮助,我们将不胜感激。
6 个解决方案
#1
64
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.
我也有同样的问题,今天我发现了一个更好的解决方案。只需将此代码附加到配置/引导中。rb和它应该和流浪汉一起工作。
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
注:它基于这个答案
#2
36
You can use foreman to run a Procfile
with your custom commands:
您可以使用foreman使用您的自定义命令运行Procfile:
# Procfile in Rails application root
web: bundle exec rails s -b 0.0.0.0
Now start your Rails application with:
现在开始您的Rails应用程序:
foreman start
The good thing about foreman is that you can add other applications to the Procfile (like sidekiq, mailcatcher).
foreman的好处是可以向Procfile中添加其他应用程序(比如sidekiq、mailcatcher)。
The bad thing about foreman is that you have to train your team to run foreman start
instead of rails s
.
工头的缺点是,你必须训练你的团队去管理工头,而不是轨道。
#3
17
Met the same problem. Found the blog Make Rails 4.2 server listens to all interfaces.
遇到了同样的问题。找到让Rails 4.2服务器监听所有接口的博客。
Add the following to config/boot.rb
添加以下配置/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
#4
6
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:
如果将默认选项放在配置/引导上。然后,所有rake和rails的命令属性都失败了(例如:rake -T或rails g模型用户)!所以,请将这个附加到bin/rails后面。/config/boot'和代码仅为rails服务器命令执行:
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:
bin/rails文件如下:
#!/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'
#5
1
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.
这里有一个更简单的解决方案。我已经喜欢/需要dotenv和puma-heroku了,所以如果用它们对你不起作用,那么这可能对你不合适。
/config/puma.rb
/ config / puma.rb
plugin :heroku
Gemfile
Gemfile
gem 'dotenv-rails', groups: [:development, :test]
.env
.env
PORT=8080
Now I can start both dev and production with rails s
.
现在我可以用rails s开始开发和生产了。
#6
0
Switch to Puma and specify port
in config/puma.rb
, e.g.:
切换到Puma并在config/ Puma中指定端口。rb,例如:
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
显然,它将绑定到指定端口的0.0.0.0:https://github.com/puma/puma/issues/896。
#1
64
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.
我也有同样的问题,今天我发现了一个更好的解决方案。只需将此代码附加到配置/引导中。rb和它应该和流浪汉一起工作。
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
注:它基于这个答案
#2
36
You can use foreman to run a Procfile
with your custom commands:
您可以使用foreman使用您的自定义命令运行Procfile:
# Procfile in Rails application root
web: bundle exec rails s -b 0.0.0.0
Now start your Rails application with:
现在开始您的Rails应用程序:
foreman start
The good thing about foreman is that you can add other applications to the Procfile (like sidekiq, mailcatcher).
foreman的好处是可以向Procfile中添加其他应用程序(比如sidekiq、mailcatcher)。
The bad thing about foreman is that you have to train your team to run foreman start
instead of rails s
.
工头的缺点是,你必须训练你的团队去管理工头,而不是轨道。
#3
17
Met the same problem. Found the blog Make Rails 4.2 server listens to all interfaces.
遇到了同样的问题。找到让Rails 4.2服务器监听所有接口的博客。
Add the following to config/boot.rb
添加以下配置/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
#4
6
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:
如果将默认选项放在配置/引导上。然后,所有rake和rails的命令属性都失败了(例如:rake -T或rails g模型用户)!所以,请将这个附加到bin/rails后面。/config/boot'和代码仅为rails服务器命令执行:
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:
bin/rails文件如下:
#!/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'
#5
1
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.
这里有一个更简单的解决方案。我已经喜欢/需要dotenv和puma-heroku了,所以如果用它们对你不起作用,那么这可能对你不合适。
/config/puma.rb
/ config / puma.rb
plugin :heroku
Gemfile
Gemfile
gem 'dotenv-rails', groups: [:development, :test]
.env
.env
PORT=8080
Now I can start both dev and production with rails s
.
现在我可以用rails s开始开发和生产了。
#6
0
Switch to Puma and specify port
in config/puma.rb
, e.g.:
切换到Puma并在config/ Puma中指定端口。rb,例如:
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
显然,它将绑定到指定端口的0.0.0.0:https://github.com/puma/puma/issues/896。