我有一些 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。
简单的解决方案是允许健康检查器用户代理,将其添加到您的
production.log
config.host_authorization = {
exclude: ->(request) { request.user_agent =~ /ELB-HealthChecker/ }
}
从
config.hosts << "hostname"
配置中删除任何 application.rb
- 那么 Rails 就不会阻止主机
看起来在最新版本中已经解决了,至少可以在 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/ }
}