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

1.8k views Asked by At

I am trying to use nginx with passenger in a docker container. If I start the rails app using passenger standalone, it will work properly:

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

However, if I tried to use nginx for the same rails app, I get this error:

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.

my 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;
    
}

Any idea?

EDIT

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
2

There are 2 answers

1
tessie On BEST ANSWER

Remove the font_assets gem.

This happens because font_assets RackMiddleWare is invoked before ActionDisptach:Static .

Also note that fon_assets gem only works if assets are not precompiled on production. Refer https://github.com/ericallam/font_assets#important .If assets are precompiled and served using nginx refer nginx config

0
akostadinov On

@tessie reply is probably the best one. You can remove the gem and use rack-cors gem or just insert headers e.g.

  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',
  }

But in case you surely want to to use font_assets, I spotted a pull request to fix the issue - https://github.com/ericallam/font_assets/pull/53