【问题标题】:Is there a way to stop Rails' built-in server from listening on 0.0.0.0 by default?有没有办法阻止 Rails 的内置服务器默认监听 0.0.0.0?
【发布时间】:2014-07-23 23:28:32
【问题描述】:

我在不受信任的网络(咖啡店、邻居的开放式 wifi、DEF CON)上进行了很多 Web 开发,当随机、肯定有问题的软件(比如我的 Rails 应用程序正在开发中)在 0.0 上绑定端口时,我会感到紧张.0.0 并开始接受所有来者的请求。我知道我可以使用 -b 选项指定绑定到服务器的地址,但我想全局更改默认值,因此除非我另有说明,否则它始终以这种方式运行。当然,我也可以运行某种会阻止连接的防火墙,但最好不要一开始就听。是否有“.railsrc”文件或类似文件——至少是每个项目的设置文件,但最好是一些全局设置文件——我可以用来强制服务器默认只绑定到 127.0.0.1?

【问题讨论】:

  • 如果您的操作系统支持别名,只需使用它来运行服务器即可。

标签: ruby-on-rails ruby security networking


【解决方案1】:

使用--binding=ip参数:

rails s --binding=127.0.0.1

https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb

【讨论】:

  • 我知道这个选项——我希望能够在全局范围内更改它,因此我不必在每次启动服务器时都指定它。
  • 您可以在应用程序的初始化程序中对github.com/rails/rails/blob/master/railties/lib/rails/commands/… 中定义的Rails::Server::Options#parse! 进行monkeypatch,甚至可以在系统的gem 中编辑该文件。
【解决方案2】:

您可以更新您的 rails 应用程序中的 /script/rails 文件以反映以下内容:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)

# START NEW CODE
require "rails/commands/server"
module Rails
  class Server
    def default_options
      super.merge({
        :Host        => 'my-host.com',
        :Port        => 3000,
        :environment => (ENV['RAILS_ENV'] || "development").dup,
        :daemonize   => false,
        :debugger    => false,
        :pid         => File.expand_path("tmp/pids/server.pid"),
        :config      => File.expand_path("config.ru")            
      })
    end
  end
end
# END NEW CODE

require 'rails/commands'

这将在启动时将 rails 应用程序绑定到 my-host.com。您仍然可以从命令行覆盖选项。

我不确定为什么 Rails::Server API 文档中没有反映这一点。您可以查看https://github.com/rails/rails/blob/master/railties/lib/rails/commands/server.rb 以查看服务器实现。

请注意,在 Rails 4 中,/script/rails 文件已移至 /bin/rails。

【讨论】:

    【解决方案3】:

    无法全局更改,您必须使用-b

    rails s -b <ip address>

    【讨论】:

    • 在 Rails 2 上:script/server -b <ip address>
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 2019-08-08
    • 2021-10-25
    • 2020-11-03
    相关资源
    最近更新 更多