All posts

Fix Load Balancer Error in Rails

Fix Rails application errors behind load balancers including ActionDispatch trusted proxies, force_ssl issues, and cable configuration.

Rails Behind a Load Balancer

Rails has built-in middleware for proxy support, but it needs to know about your load balancer. Common symptoms include redirect loops with force_ssl, wrong request.remote_ip, and broken ActionCable.

Redirect Loop with force_ssl

When the load balancer terminates SSL and forwards HTTP to Rails, config.force_ssl = true creates an infinite redirect. Fix it by trusting the proxy header:

# config/environments/production.rb
config.force_ssl = true
config.ssl_options = { redirect: { exclude: ->(request) {
  request.path.start_with?('/healthz')
}}}

Rails reads X-Forwarded-Proto automatically through ActionDispatch::RemoteIp, so it should detect HTTPS. If it doesn't, check that your load balancer sends the header.

Custom Trusted Proxies

# config/application.rb
config.action_dispatch.trusted_proxies = ActionDispatch::RemoteIp::TRUSTED_PROXIES + [
  IPAddr.new('10.0.0.0/8'),
  IPAddr.new('172.16.0.0/12'),
]

ActionCable Through the Load Balancer

# config/cable.yml
production:
  adapter: redis
  url: redis://redis-service:6379/1

Configure your load balancer to support WebSocket upgrades on /cable.

Health Check

# config/routes.rb
get '/healthz', to: proc { [200, {}, ['ok']] }

This bypasses the full Rails middleware stack for minimal latency.

Bugsly integrates with Rails via a Rack middleware that captures exceptions with the correct remote_ip after proxy header processing.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free