ruby-on-railsactiondispatch

ActionDispatch::Static in Rails production environment


I'm looking to use ActionDispatch::Static to serve static files in a directory other than public. It works perfectly in development, but I can't seem to get the same results in production.

I have my static files in /var/www/mysite.com-static. In an initializer, I've added the following:

Rails.application.config.middleware.insert_after Rack::SendFile, ActionDispatch::Static, '/var/www/mysite.com-static'

The output of rake middleware is as follows:

use Raven::Rack
use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000005e61130>
use Rack::Runtime
...

Why do I get 404s on every page I request within /var/www/myapp.com-static?


Solution

  • I ended up solving this problem adding the middleware in my Rails app's config.ru.

    require ::File.expand_path('../config/environment',  __FILE__)
    
    # Added the following line
    use ActionDispatch::Static, '/var/www/myapp.com-static'
    
    run Rails.application
    

    This worked because I was setting the path for ActionDispatch::Static in an initializer that was loaded after the middleware was already mounted. If I had correctly configured my load order, Rails.application.config.middleware would have also worked.