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/1Configure 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 FreeRelated Articles
How to Fix Permissionerror in FastAPI In Production
Learn how to diagnose and fix the permissionerror in FastAPI in production. Includes code examples and prevention tips.
Read moreFix AuthenticationError Error in Swift — When Deploying
Learn how to fix the AuthenticationError error in Swift when deploying. Step-by-step guide with code examples and solutions.
Read moreHow to Fix Referenceerror in Kotlin When Deploying
Learn how to diagnose and fix the referenceerror in Kotlin when deploying. Includes code examples and prevention tips.
Read moreFix Session Error in Flutter
Step-by-step guide to fix Session Error in Flutter. Includes root cause analysis, code examples, debugging tips, and prevention strategies.
Read more