Hua*_*Tao 85 ruby-on-rails ruby-on-rails-4.2
我们团队的Rails应用程序升级至4.2,为以后发行说明中提到,默认IP rails server绑定到更改为localhost从0.0.0.0.
我们使用Vagrant开发,并希望可以直接从主机上的浏览器访问开发服务器.
rails s -b 0.0.0.0我不知道从现在开始每次都打字,我想知道是否有更优雅的解决方案,这样我们仍然可以使用简单的rails s启动服务器.也许:
rails s读取我可以在哪里修改默认绑定ip(不使用-c)这背后的真正目标是我希望我们的团队之间的升级顺利进行,避免由于缺少-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
Run Code Online (Sandbox Code Playgroud)
在官方文档中未找到任何相关说明.任何帮助将不胜感激.
小智 70
我在这里遇到同样的问题,今天我发现了一个更好的解决方案.只需将此代码附加到您的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
Run Code Online (Sandbox Code Playgroud)
ps:它基于:这个答案
zwi*_*pie 45
您可以使用foreman使用Procfile自定义命令运行a :
# Procfile in Rails application root
web: bundle exec rails s -b 0.0.0.0
Run Code Online (Sandbox Code Playgroud)
现在启动您的Rails应用程序:
foreman start
Run Code Online (Sandbox Code Playgroud)
关于foreman的好处是你可以将其他应用程序添加到Procfile(如sidekiq,mailcatcher).
工头的坏处是你必须训练你的团队foreman start而不是rails s.
Bia*_*ing 19
遇到了同样的问题.发现博客Make Rails 4.2服务器监听所有接口.
将以下内容添加到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
Run Code Online (Sandbox Code Playgroud)
jsm*_*rtt 11
对于带有 Puma 3.12.1 的 Rails 5.1.7,所选答案不起作用,但我通过将以下内容添加到我的config/puma.rb文件中来完成它:
set_default_host '0.0.0.0' # Note: Must come BEFORE defining the port
port ENV.fetch('PORT') { 3000 }
Run Code Online (Sandbox Code Playgroud)
我通过检查dsl 文件确定了这一点。它用于instance_eval该文件,因此可能还有其他方法可以做到这一点,但这对我来说似乎是最合理的。
如果你设置了默认选项,config/boot.rb那么rake和rails的所有命令属性都会失败(例如:rake -T或rails g model user)!因此,将其附加到bin/rails行后,require_relative '../config/boot'并且仅针对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
Run Code Online (Sandbox Code Playgroud)
该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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
56166 次 |
| 最近记录: |