如何更改 Rails 4.2 开发服务器的默认绑定 ip? [英] How to change the default binding ip of Rails 4.2 development server?

查看:18
本文介绍了如何更改 Rails 4.2 开发服务器的默认绑定 ip?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我们团队的 Rails 应用程序升级到 4.2 后,如 发行说明提到,rails server绑定的默认ip从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一样简单地使用sth 启动服务器.也许:

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆