首页 文章

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

提问于
浏览
76

将我们团队的rails应用程序升级到4.2之后,如release note所述,默认的ip rails server 绑定从 0.0.0.0 更改为 localhost .

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

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

  • 配置文件 rails s 读取我可以修改默认绑定ip的位置(不使用 -c

  • port with vagrant(尝试但失败了,见下面遇到的问题)

  • 一个猴子补丁到机架,改变默认绑定ip

这背后的真正目标是我希望我们的团队之间的升级顺利进行,避免由于缺少 -b 0.0.0.0 部分而导致人们不断重启其rails服务器的故障 .

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

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

没有在官方文档中找到任何相关说明 . 任何帮助将不胜感激 .

6 回答

  • 6

    这里's a simpler solution that I'米使用 . 我已经喜欢/需要dotenvpuma-heroku,所以如果使用那些不适合你,那么这可能不适合你 .

    /config/puma.rb

    plugin :heroku
    

    Gemfile

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

    .env

    PORT=8080
    

    现在我可以用 rails s 启动开发和 生产环境 .

  • 1

    我在这里遇到同样的问题,今天我发现了一个更好的解决方案 . 只需将此代码附加到您的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
    

    ps:它基于:this answer

  • 18

    您可以使用foreman使用自定义命令运行 Procfile

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

    现在启动您的Rails应用程序:

    foreman start
    

    关于foreman的好处是你可以将其他应用程序添加到Procfile(如sidekiq,mailcatcher) .

    工头的坏处是你必须训练你的团队运行 foreman start 而不是 rails s .

  • 64

    遇到同样的问题 . 找到了博客Make Rails 4.2 server listens to all interfaces .

    将以下内容添加到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
    
  • 43

    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)! 因此,在 require_relative '../config/boot' 行之后将其附加到 bin/rails ,并且仅针对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
    

    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'
    
  • 1

    切换到Puma并在 config/puma.rb 中指定 port ,例如:

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

    显然它将绑定到指定端口的0.0.0.0:https://github.com/puma/puma/issues/896

相关问题