Rails 生产无法在 AWS 负载均衡器中运行

T.K*_*Kul 0 ruby-on-rails amazon-elb

我的 Rails 6 应用程序可以在 EC2 实例中的开发模式下正常工作。但是当配置使用生产模式时。负载均衡器无法进行运行状况检查,也无法运行应用程序。

我的健康检查:

在此输入图像描述

安全性:负载均衡器

在此输入图像描述

在此输入图像描述

安全性:Rails 应用程序

在此输入图像描述

在此输入图像描述

负载均衡器正在开发中

在此输入图像描述

这里是与负载均衡器一起使用的开发

启动导轨:

rails s -p 3000 -b 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

然后回应

=> Booting Puma
=> Rails 6.0.3.2 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.5 (ruby 2.6.3-p62), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Run Code Online (Sandbox Code Playgroud)

配置/环境/development.rb

Rails.application.configure do
  config.hosts << "xxxxxxxx.us-east-2.elb.amazonaws.com" #This is public dns of load balance
  config.cache_classes = false
  config.eager_load = false
  config.consider_all_requests_local = true
  if Rails.root.join('tmp', 'caching-dev.txt').exist?
    config.action_controller.perform_caching = true
    config.action_controller.enable_fragment_cache_logging = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end
  config.action_mailer.raise_delivery_errors = false
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

  config.action_mailer.perform_caching = false
  config.active_support.deprecation = :log
  config.assets.debug = true
  config.assets.quiet = true
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end
Run Code Online (Sandbox Code Playgroud)

以下是生产(不工作)

配置/环境/生产.rb

启动导轨:

RAILS_ENV=production rails s -p 3000 -b 0.0.0.0
Run Code Online (Sandbox Code Playgroud)

然后回应:

=> Booting Puma
=> Rails 6.0.3.2 application starting in production 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.5 (ruby 2.6.3-p62), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: production
* Listening on tcp://0.0.0.0:3000


Rails.application.configure do
  config.hosts << "xxxxxxxx.us-east-2.elb.amazonaws.com" #This is public dns of load balance
  config.hosts << "3.14.65.84"
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
  config.assets.compile = false
  config.log_level = :debug
  config.log_tags = [ :request_id ]
  config.action_mailer.perform_caching = false
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
  if ENV["RAILS_LOG_TO_STDOUT"].present?
    logger           = ActiveSupport::Logger.new(STDOUT)
    logger.formatter = config.log_formatter
    config.logger    = ActiveSupport::TaggedLogging.new(logger)
  end
end
Run Code Online (Sandbox Code Playgroud)

负载均衡器:运行状况检查不起作用!

在此输入图像描述

我也尝试过:

  1. 将 config/environments/development.rb 复制到 production.rb,然后作为生产环境运行结果 =====> 健康检查不起作用!
  2. 将 config/environment/production.rb 复制到development.rb,然后作为开发环境运行结果 =====> 健康检查工作!

似乎与 Rails 配置无关,而是它在 AWS 中处理生产的方式

帮助:如何使用负载均衡器使 Rails 6 在 AWS EC2 中作为生产环境工作?

小智 8

我的公司刚刚遇到了一个听起来非常相似的问题。一旦 ECS 启动了任务,我们就可以通过 ELB 访问 Rails 应用程序,但运行状况检查将失败,并且它会自动关闭它尝试启动的每个容器。

我们最终将 IP 范围添加到主机配置中。在生产中完全禁用它感觉不太好,所以我们得出了类似于这样的结果:

config.hosts = [
  "publicdomain.com",
  "localhost",
  IPAddr.new("10.X.X.X/23")
]
Run Code Online (Sandbox Code Playgroud)

列入白名单的 IP 地址与 ECS 在容器中创建和分配时将使用的范围相匹配。希望这有帮助!