繁体   English   中英

如何防止 Rails 6 阻塞我的 AWS EC2 实例?

[英]How to prevent Rails 6 from blocking my AWS EC2 instances?

我有一些 Rails 6 应用程序,通过 Opsworks 部署在 AWS 上。

升级到 Rails 6 后,应用程序会阻止对其自身实例的健康检查,并导致负载均衡器关闭该实例。

我想知道如何使用动态 IP 地址自动将我所有的 EC2 实例列入白名单? 而不是一个一个地添加到config/application.rb?

谢谢

Rails.application.configure do
  # Whitelist one hostname
  config.hosts << "hostname"
  # Whitelist a test domain
  config.hosts << /application\.local\Z/
  # config.hosts.clear 
end

对我有用的解决方法是

config.hosts.clear

我不久前发布了这个问题。 更安全的解决方案是从可以从 AWS 控制台设置的环境变量中读取 IP 地址。

config.hosts << ENV["INSTANCE_IP"]
config.hosts << ENV["INSTANCE_IP2"]
...
config.hosts << ENV["INSTANCE_IPn"]

至少通过这种方式,当实例具有动态 IP 时,每次 IP 地址更改时都不需要新的 git commit。

简单的解决方案是允许 Health Checker 用户代理,将其添加到您的production.log

  config.host_authorization = {
    exclude: ->(request) { request.user_agent =~ /ELB-HealthChecker/ }
  }

看起来它已在最新版本中得到解决至少适用于 6.1 及更高版本

https://guides.rubyonrails.org/configuring.html#actiondispatch-hostauthorization

您可以通过设置 config.host_configuration.exclude 从主机授权检查中排除某些请求:

# Exclude requests for the /healthcheck/ path from host checking
Rails.application.config.host_configuration = {
  exclude: ->(request) { request.path =~ /healthcheck/ }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM