【问题标题】:Rails 6 Binds to 127.0.0.1 by Default?Rails 6 默认绑定到 127.0.0.1?
【发布时间】:2020-04-05 15:35:22
【问题描述】:

我在看一个(虽然很老)security talk on insecure rails defaults。我想知道是否有办法判断 Rails 现在是否默认绑定到 ip 127.0.0.1,端口 3000?当我启动 rails s 时,我看到了

    ±  |master {1} U:4 ✗| → rails s
=> Booting Puma
=> Rails 6.0.2.2 application starting in development 
=> Run `rails server --help` for more startup options
/Users/pivotal/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/pivotal/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.3 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://**127.0.0.1**:3000
* Listening on tcp://[::1]:3000
Use Ctrl-C to stop

根据pretty old Stack overflow post,我尝试将以下内容添加到我的 boot.rb 中,但我的 Spring 版本出现错误。

引导.rb 需要'rails/commands/server'

module Rails
  class Server
    def default_options
      super.merge({Port: 10524, Host: '127.0.0.1'})
    end
  end
end


    ±  |master {1} U:4 ✗| → rails s
You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).

This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system. To solve this, upgrade your bundle to the latest Spring version and then run `bundle exec spring binstub --all` to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.

我执行了这些步骤,但仍然出错。

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-6


【解决方案1】:

您正在使用 Rails 6,最新版本使用 puma,您的应用程序也是如此。如果你想改变端口打开config/puma.rb。在那里你会看到这条线

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

这条线是让 rails 从端口 3000 开始的原因。将其更改为任何其他端口。在你的情况下 -

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

另外,如果您想更改绑定地址,请将port 替换为bind

#port        ENV.fetch("PORT") { 3000 }
bind        'tcp://192.168.0.1:10524'

查看https://github.com/puma/puma了解更多信息。

【讨论】:

    猜你喜欢
    • 2015-07-08
    • 2022-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    相关资源
    最近更新 更多