ipegasus
ipegasus

Reputation: 15262

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

I have some Rails 6 applications, deployed at AWS, via Opsworks.

After upgrading to Rails 6 the app blocks the health check of its own instance and it causes the load balancer to take the instance down.

I would like to know how to whitelist all my EC2 instances automatically with dynamic IP addresses? Instead of adding one by one to config/application.rb?

Thanks

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

Upvotes: 6

Views: 2378

Answers (5)

Daniel Garmoshka
Daniel Garmoshka

Reputation: 6362

remove any config.hosts << "hostname" from application.rb config - then rails won't block hosts

Upvotes: 1

Arthur Neves
Arthur Neves

Reputation: 12138

Simple solution is to allow the Health Checker user agent, add this to your production.log

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

Upvotes: 0

coderVishal
coderVishal

Reputation: 9109

Looks like it has been resolved in the latest versions atleast works on 6.1 and above

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

You can exclude certain requests from Host Authorization checks by setting config.host_configuration.exclude:

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

Upvotes: -1

ipegasus
ipegasus

Reputation: 15262

I posted this question a while back. A safer solution would be reading the IP addresses from environmental variables that can be set from the AWS console.

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

At least in this way it does not require a new git commit every time the IP address changes when the instance has a dynamic IP.

Upvotes: 1

Ariel Cabib
Ariel Cabib

Reputation: 2102

The work-around that worked for me was

config.hosts.clear

Upvotes: 3

Related Questions