问题描述
将我们团队的 rails 应用程序升级到 4.2 后,如发行说明提到,默认 ip rails server
绑定到的从 0.0.0.0
更改为 localhost
.
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
.
我们使用 Vagrant 进行开发,并希望可以直接从主机上的浏览器访问开发服务器.
We develop with Vagrant, and want the development server to be accessible directly from browser on the host machine.
以后不再每次都输入rails s -b 0.0.0.0
,不知道有没有更优雅的解决方案,让我们仍然可以像rails s那样简单
启动服务器.也许:
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
读取我可以修改默认绑定 ip 的位置(不使用-c
) - 使用 vagrant 转发(尝试但失败,请参阅下面遇到的问题)
- 一个用于机架的猴子补丁,它改变了默认的绑定 ip
- a config file
rails s
reads where I can modify the default binding ip (without using-c
) - port forward with vagrant (tried but failed, see problem encountered below)
- a monkey patch to rack, that changes the default binding ip
这背后的真正目标是我希望我们的团队能够顺利进行升级,避免人们由于缺少 -b 0.0.0.0
而不得不不断重启他们的 Rails 服务器的故障部分.
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.
我尝试了 vagrant 端口转发,但是当我在主机上访问 localhost:3000
时仍然得到 Connection Refused
.我试过的两行配置是:
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:
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.
推荐答案
我在这里遇到了同样的问题,今天我找到了一个更好的解决方案.只需将此代码附加到您的 config/boot.rb 中,它应该可以与 vagrant 一起使用.
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:它基于:这个答案
这篇关于Rails 4.2开发服务器如何修改默认绑定ip?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!