0

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?

Ray Zane
  • 236
  • 1
  • 4
  • 10
  • If nginx serves your static assets, then why are you trying to handle it with help of rails app? – Stanislav Mekhonoshin May 20 '15 at 16:34
  • nginx serves my static assets in /var/www/myapp.com/public. I'd also like to serve static assets in /var/www/myapp.com-static. As far as I know, nginx only allows you to specify one root (without using a location). – Ray Zane May 20 '15 at 16:37

1 Answers1

0

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.

Ray Zane
  • 236
  • 1
  • 4
  • 10
  • That's very odd. While there are some differences between the two, not sure why adding ActionDispatch::Static in a different place would make a difference here. – Noah Gibbs May 20 '15 at 20:13
  • Yes, I thought so too. The strangest part is that using config.middleware.use works fine in dev and test, but refused to work in production. – Ray Zane May 21 '15 at 15:54
  • The obvious reason for this to work in dev but not production is if NGinX is trying to serve a file from the wrong place and can't. So since you're serving static files, that's not quite *as* weird :-) – Noah Gibbs May 21 '15 at 19:18
  • Could you elaborate? I'm not sure what you mean. – Ray Zane May 21 '15 at 20:17
  • The obvious failure mode for this would be if NGinX recognized the directory, tried to serve the file from a static dir, and served a 404 before your app got a chance to try. – Noah Gibbs May 22 '15 at 01:20