Persistent 502 Bad Gateway Error on Rails App - Full Configuration Applied

kamkara
HOBBY

12 days ago

Context:

I am deploying a Rails 8 application on Railway, project ID: fe17b8c2-82eb-490b-8fde-b5d4922f39c0.
The deployment completes successfully, database migrations run without errors, but accessing the app results in:

502 Bad Gateway - Application failed to respond

What I've already checked and implemented:

Procfile at project root:

web: bin/rails server -b 0.0.0.0 -p ${PORT:-8080}

config/environments/production.rb:

config.hosts = [
  "acceleratewithai.com",
  /.*\.acceleratewithai\.com/,
  /.*\.up\.railway\.app/
]

config/database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  port: 5432

production:
  primary: &primary_production
    <<: *default
    url: <%= ENV["DATABASE_URL"] %>
    database: <%= ENV["DATABASE_NAME"] %>
    username: <%= ENV["DATABASE_USERNAME"] %>
    password: <%= ENV["DATABASE_PASSWORD"] %>
    host: <%= ENV["DATABASE_HOST"] %>

Environment Variables Set on Railway:

  • DATABASE_URL

  • DATABASE_NAME

  • DATABASE_USERNAME

  • DATABASE_PASSWORD

  • DATABASE_HOST

  • RAILS_MASTER_KEY

Railway port configured to 8080
SSL certificate active via Railway
DNS is correctly pointing to Railway
Migrations run successfully during deployment

Current Problem:

  • Application is unreachable via both the custom domain (acceleratewithai.com) and the Railway-provided domain

  • Consistently getting 502 Bad Gateway - Application failed to respond

  • Deployment logs show no errors, migrations succeed

  • Application remains inaccessible even after a full redeployment

Additional Information:

  • I plan to fully delete and redeploy the project once I find the correct solution

  • Stack: Rails 8, PostgreSQL (via Railway plugin)

  • All environment variables properly defined in Railway dashboard

  • Using standard Puma server with proper Procfile setup

Request for Support:

  • Could there be any Railway-specific requirement I may have overlooked for Rails apps?

  • Is there a potential conflict using both url: <%= ENV["DATABASE_URL"] %> and individual fields like database, username, etc. in database.yml?

  • Can Railway provide additional logs or diagnostics to pinpoint why the container is considered "non-responsive" despite passing deployment?

Thank you for your assistance. I'm available to provide further details if needed.

Solved$10 Bounty

2 Replies

colinrm000
HOBBYTop 10% Contributor

12 days ago

Railway is waiting for something to listen on the random $PORT it injects.

Your container boots, runs migrations, then Rails starts in development on port 3000/8080, so Railway never sees it – hence the 502.

To fix,
Run Puma directly (and in production)

Procfile should be one line, nothing else.

Ex: web: bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:${PORT}

( rails server defaults to development and sometimes never binds on $PORT, so Railway thinks the container is dead.)

Also, Set env RAILS_ENV=production (and optionally RAILS_SERVE_STATIC_FILES=true).

For your question "Is there a potential conflict using both url: <%= ENV["DATABASE_URL"] %> and individual fields like database, username, etc. in database.yml?"

In database.yml just do

   production:
     url: <%= ENV["DATABASE_URL"] %>

(don’t mix URL + individual fields).

Now once you redeploy, logs should show “Puma starting in production on tcp://0.0.0.0:xxxxx” and the 502 should go away.


colinrm000

Railway is waiting for something to listen on the random $PORT it injects.Your container boots, runs migrations, then Rails starts in development on port 3000/8080, so Railway never sees it – hence the 502.To fix,Run Puma directly (and in production)Procfile should be one line, nothing else.Ex: web: bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:${PORT}( rails server defaults to development and sometimes never binds on $PORT, so Railway thinks the container is dead.)Also, Set env RAILS_ENV=production (and optionally RAILS_SERVE_STATIC_FILES=true).For your question "Is there a potential conflict using both url: <%= ENV["DATABASE_URL"] %> and individual fields like database, username, etc. in database.yml?"In database.yml just doproduction: url: <%= ENV["DATABASE_URL"] %>(don’t mix URL + individual fields).Now once you redeploy, logs should show “Puma starting in production on tcp://0.0.0.0:xxxxx” and the 502 should go away.

kamkara
HOBBY

11 days ago

Update: The app works when I set the domain port to 3000 instead of 8080 on Railway.

So for now, everything runs correctly with the domain configured on port 3000.

Thank you for your help!


Status changed to Solved brody 11 days ago