部署在Heroku上的Ruby on Rails应用启动失败,显示应用错误
问题
已成功将Ruby on Rails应用部署到Heroku,但应用无法启动,显示Application error。最初遇到「Failed to install gems via Bundler」错误,通过执行命令bundle lock --add-platform x86_64-linux,提交至Git并更新到Heroku后解决了该问题,但应用仍无法启动。作为Heroku部署新手,难以从日志中定位错误,以下是获取到的包含503状态的H10(App crashed)错误日志:
2022-08-09T06:31:41.767338+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=examplerails.herokuapp.com request_id=981a1409-1e70-492c-a2ea-081a06f72ecf fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:31:43.201958+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=examplerails.herokuapp.com request_id=8bc32463-dd26-4a1e-bc7f-483b72d91fa4 fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:34:41.603649+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=examplerails.herokuapp.com request_id=1b7339b2-9706-479e-8ba9-3a29392a05a8 fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:34:41.882759+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=examplerails.herokuapp.com request_id=a6ad6b2c-2cb5-4fd9-a803-1fc1ebf36b17 fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:43:46.049127+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=examplerails.herokuapp.com request_id=c282cc3b-2f7e-4010-9f96-ddc996c87abd fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:43:46.314701+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=examplerails.herokuapp.com request_id=3a12bb5a-2b46-433c-997e-a9b6bcdcb18f fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:48:44.746660+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=examplerails.herokuapp.com request_id=6c61c7ce-f69e-43de-aed5-40d5f85239a1 fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https 2022-08-09T06:48:45.250757+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=examplerails.herokuapp.com request_id=b4656eff-d125-4225-90fd-fb8e14cdf5ea fwd="102.89.41.87" dyno= connect= service= status=503 bytes= protocol=https
还有一段包含config.ru调用栈的日志:
from config.ru:3:in `require_relative' 2022-08-09T02:17:27.936748+00:00 app[web.1]: from config.ru:3:in `block in <main>' 2022-08-09T02:17:27.936755+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/builder.rb:116:in `eval' 2022-08-09T02:17:27.936764+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/builder.rb:116:in `new_from_string' 2022-08-09T02:17:27.936766+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/builder.rb:105:in `load_file' 2022-08-09T02:17:27.936773+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/builder.rb:66:in `parse_file' 2022-08-09T02:17:27.936775+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:349:in `build_app_and_options_from_config' 2022-08-09T02:17:27.936776+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:249:in `app' 2022-08-09T02:17:27.936791+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:422:in `wrapped_app' 2022-08-09T02:17:27.936791+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:312:in `block in start' 2022-08-09T02:17:27.936792+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:379:in `handle_profiling' 2022-08-09T02:17:27.936794+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/server.rb:311:in `start' 2022-08-09T02:17:27.936794+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/commands/server/server_command.rb:38:in `start' 2022-08-09T02:17:27.936803+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/commands/server/server_command.rb:143:in `block in perform' 2022-08-09T02:17:27.936805+00:00 app[web.1]: from <internal:kernel>:90:in `tap' 2022-08-09T02:17:27.936807+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/commands/server/server_command.rb:134:in `perform' 2022-08-09T02:17:27.936809+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run' 2022-08-09T02:17:27.936817+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command' 2022-08-09T02:17:27.936819+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch' 2022-08-09T02:17:27.936820+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/command/base.rb:87:in `perform' 2022-08-09T02:17:27.936828+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/command.rb:48:in `invoke' 2022-08-09T02:17:27.936830+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/commands.rb:18:in `<main>' 2022-08-09T02:17:27.936837+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' 2022-08-09T02:17:27.936839+00:00 app[web.1]: from /app/vendor/bundle/ruby/3.1.0/gems/bootsnap-1.13.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:32:in `require' 2022-08-09T02:17:27.936841+00:00 app[web.1]: from bin/rails:4:in `<main>'
请问是否是这段日志导致应用无法启动?
回答
这两段日志不是导致应用崩溃的原因,它们只是崩溃后的结果提示:
- H10错误日志只是Heroku路由器返回的状态,说明应用已经崩溃,无法处理请求,本质是结果而非原因。
- 你提供的config.ru调用栈日志不完整,缺少最关键的错误信息(比如
LoadError、NameError这类具体异常)。正常的错误调用栈会在顶部显示触发崩溃的具体错误,比如找不到某个文件、数据库配置错误、依赖缺失等,你现在只拿到了调用栈的后半部分。
排查步骤
- 执行
heroku logs --tail获取完整的实时日志,重点看应用启动阶段(app[web.1]开头的日志)的错误信息,这部分会包含触发崩溃的具体原因。 - 检查Rails的环境配置:
- 确认
config/database.yml中的生产环境配置是否符合Heroku的要求(Heroku会自动注入DATABASE_URL环境变量,无需手动配置数据库账号密码)。 - 确保生产环境所需的gem都已包含在
Gemfile的:production组中,且没有遗漏必要的依赖。
- 确认
- 执行
heroku run rails console尝试启动控制台,如果启动失败,控制台会直接输出错误信息,这是定位问题的快捷方式。 - 检查是否有未执行的数据库迁移:执行
heroku run rails db:migrate,确保生产环境数据库结构和代码一致。
内容的提问来源于stack exchange,提问作者user19707340
相关产品推荐
相关产品推荐

