之前没有要插入的中间件:ActionDispatch::Static (RuntimeError) 仅在使用 Nginx 时

No such middleware to insert before: ActionDispatch::Static (RuntimeError) ONLY when using Nginx

我正在尝试在 docker 容器中将 nginx 与 passenger 一起使用。 如果我使用乘客独立启动 rails 应用程序,它将正常工作:

bundle exec passenger start --engine=builtin --max-pool-size=6 --min-instances=3

但是,如果我尝试将 nginx 用于同一个 rails 应用程序,我会收到此错误:

App 21674 output: Using /usr/local/rvm/gems/ruby-2.6.6
App 21674 output: I, [2020-09-14T05:02:50.855329 #21674]  INFO -- sentry: ** [Raven] Raven 2.13.0 ready to catch errors
App 21674 output: I, [2020-09-14T05:02:51.103254 #21674]  INFO -- : Installing Passenger worker loop.
App 21674 output: Error: The application encountered the following error: No such middleware to insert before: ActionDispatch::Static (RuntimeError)
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/actionpack-5.2.4.1/lib/action_dispatch/middleware/stack.rb:108:in `assert_index'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/actionpack-5.2.4.1/lib/action_dispatch/middleware/stack.rb:75:in `insert'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/configuration.rb:71:in `block in merge_into'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/configuration.rb:70:in `each'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/configuration.rb:70:in `merge_into'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/engine.rb:509:in `block in app'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/engine.rb:506:in `synchronize'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/engine.rb:506:in `app'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `instance_exec'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `run'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/initializable.rb:61:in `block in run_initializers'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:228:in `block in tsort_each'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:431:in `each_strongly_connected_component_from'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:349:in `block in each_strongly_connected_component'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `each'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `call'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:347:in `each_strongly_connected_component'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:226:in `tsort_each'
App 21674 output:     /usr/local/rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/tsort.rb:205:in `tsort_each'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/initializable.rb:60:in `run_initializers'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/application.rb:361:in `initialize!'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/railtie.rb:190:in `public_send'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/railties-5.2.4.1/lib/rails/railtie.rb:190:in `method_missing'
App 21674 output:     /home/app/webapp/config/environment.rb:7:in `<top (required)>'
App 21674 output:     config.ru:3:in `require'
App 21674 output:     config.ru:3:in `block in <main>'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/rack-2.1.2/lib/rack/builder.rb:71:in `instance_eval'
App 21674 output:     /home/app/webapp/vendor/ruby/2.6.0/gems/rack-2.1.2/lib/rack/builder.rb:71:in `initialize'
App 21674 output:     config.ru:1:in `new'
App 21674 output:     config.ru:1:in `<main>'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:101:in `eval'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:101:in `preload_app'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:189:in `block in <module:App>'
App 21674 output:     /usr/lib/ruby/vendor_ruby/phusion_passenger/loader_shared_helpers.rb:378:in `run_block_and_record_step_progress'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:188:in `<module:App>'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:30:in `<module:PhusionPassenger>'
App 21674 output:     /usr/share/passenger/helper-scripts/rack-preloader.rb:29:in `<main>'
[ E 2020-09-14 05:02:52.8967 20550/Tt age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /home/app/webapp: The application encountered the following error: No such middleware to insert before: ActionDispatch::Static (RuntimeError)
  Error ID: 24ac5443
  Error details saved to: /tmp/passenger-error-iabMgj.html

[ E 2020-09-14 05:02:52.9028 20550/Tc age/Cor/Con/CheckoutSession.cpp:276 ]: [Client 3-1] Cannot checkout session because a spawning error occurred. The identifier of the error is 24ac5443. Please see earlier logs for details about the error.

我的 nginx webapp.conf:

server {
    listen 80 default_server;

    root /home/app/webapp/public;
    
    passenger_app_env production;
    passenger_enabled on;
    passenger_user app;
    
    passenger_ruby /usr/bin/ruby2.6;
    
}

有什么想法吗?

编辑

oot@0c78d3e01f5b:/home/app/webapp# bundle exec rake middleware
I, [2020-09-14T08:22:47.642428 #27056]  INFO -- sentry: ** [Raven] Raven 2.13.0 ready to catch errors
use Raven::Rack
use Rack::Sendfile
use FontAssets::Middleware
use ActionDispatch::Static
use ActionDispatch::Executor
use Dragonfly::CookieMonster
use ActiveSupport::Cache::Strategy::LocalCache::Middleware
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use ActionDispatch::RemoteIp
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ContentSecurityPolicy::Middleware
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Rack::TempfileReaper
use Warden::Manager
use Rack::Deflater
use GitRevisionMiddleware
use ScoutApm::Middleware
use OmniAuth::Builder
use OmniAuth::Builder
use OmniAuth::Strategies::Facebook
run MYRAILSAPP::Application.routes
root@0c78d3e01f5b:/home/app/webapp# RAILS_ENV=production bundle exec rake middleware
I, [2020-09-14T08:23:09.023311 #27289]  INFO -- sentry: ** [Raven] Raven 2.13.0 ready to catch errors
use Raven::Rack
use Rack::Sendfile
use FontAssets::Middleware
use ActionDispatch::Static
use ActionDispatch::Executor
use Dragonfly::CookieMonster
use ActiveSupport::Cache::Strategy::LocalCache::Middleware
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use ActionDispatch::RemoteIp
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ContentSecurityPolicy::Middleware
use Rack::Head
use Rack::ConditionalGet
use Rack::ETag
use Rack::TempfileReaper
use Warden::Manager
use Rack::Deflater
use GitRevisionMiddleware
use ScoutApm::Middleware
use OmniAuth::Builder
use OmniAuth::Builder
use OmniAuth::Strategies::Facebook
run MYRAILSAPP::Application.routes

删除 font_assets gem.

发生这种情况是因为 font_assets RackMiddleWare 在 ActionDisptach:Static 之前被调用。

另请注意,fon_assets gem 仅在资产未在生产中预编译时才有效。参考https://github.com/ericallam/font_assets#important .If assets are precompiled and served using nginx refer nginx 配置

@tessie 的回复可能是最好的。您可以删除 gem 并使用 rack-cors gem 或仅插入 headers 例如

  config.public_file_server.headers = {
    'Cache-Control' => "public, max-age=#{(config.assets.digest ? 1.year : 1.minute).to_i}",
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Headers' => 'x-requested-with',
    'Access-Control-Allow-Methods' => 'GET',
    'Access-Control-Max-Age' => '3628800',
  }

但如果你确实想使用 font_assets,我发现了一个 pull request 来解决这个问题 - https://github.com/ericallam/font_assets/pull/53