无法使用 Rails 6 在生产中加载资产资源 (css + js)

Can't load assets ressources (css + js) in production with Rails 6

当我在生产模式下启动我的应用程序时,资产 (css + js) 无法正确加载。

我进入浏览器的控制台 net::ERR_ABORTED 404 (Not Found) 或 Rails 日志 ActionController::RoutingError (No route matches [GET] "/packs/js/application-51bdb73d26ac893c3407.js"):

我做了 rake assets:precompile 并且在 environments/production.rb 中也转了 config.assets.compile = true 但结果相同

对于生产模式,我使用相同的应用程序和相同的目录,我只是将 -e production 作为参数添加到 rails server 命令

似乎您应该将 RAILS_SERVE_STATIC_FILES 环境变量设置为 true

建议是基于这个issue comment我在解决同样的问题时发现的

这只影响 config/environments/production.rb 的配置:

  # Disable serving static files from the `/public` folder by default since
  # Apache or NGINX already handles this.
  config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?