Rails 404 error for Stylesheet or JavaScript files
Rails can't load (404 error) CSS & JS files on production but has no problem loading them in development.
I'm using Capistrano for deployment and running Rails 3. My path on development is /www/myapp but my path on production is /www/myapp/current.
The application itself seems to work fine, so the issue seems to be isolated to CSS/JS files.
I tried setting the RAILS_ROOT variable to /www/myapp/current in environments/production.rb but it did not make any difference, the files still don't load.
Here's the full stack from the production log:
Started GET "/stylesheets/scaffold.css?1280867531" for 98.173.61.21 at 2010-08-04 17:04:05 -0700
ActionController::RoutingError (No route matches "/stylesheets/scaffold.css"):
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/actionpack-3.0.0.beta4/lib/action_dispatch/middleware/show_exceptions.rb:55:in `call'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/rack/logger.rb:14:in `call'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/runtime.rb:17:in `call'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/application.rb:145:in `call'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/railties-3.0.0.beta4/lib/rails/application.rb:81:in `method_missing'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:642:in `process_client'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:715:in `block in worker_loop'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:713:in `each'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:713:in `worker_loop'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:604:in `block (2 levels) in spawn_missing_workers'
/usr/local/rvm/gems/ruby-1.9.2-rc2/g开发者_如何学Pythonems/unicorn-1.1.2/lib/unicorn.rb:601:in `fork'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:601:in `block in spawn_missing_workers'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:597:in `each'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:597:in `spawn_missing_workers'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:611:in `maintain_worker_count'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:270:in `start'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/lib/unicorn.rb:29:in `run'
/usr/local/rvm/gems/ruby-1.9.2-rc2/gems/unicorn-1.1.2/bin/unicorn:123:in `<top (required)>'
/usr/local/rvm/gems/ruby-1.9.2-rc2/bin/unicorn:19:in `load'
/usr/local/rvm/gems/ruby-1.9.2-rc2/bin/unicorn:19:in `<main>'
The error message looks like your Rails app is getting the request for a static file. Rails 3 does not serve static files by default, since the webserver can do so much better. You should check your webserver's configuration. It should be configured to first look, if a static file exists in the public directory for a request and only forward the request to the Rails app, if there's no static file.
Alternatively, you can enable Rails to also serve static files with config.serve_static_assets = true
in config/environments/production.rb. However, this is not recommended in production, since you really shouldn't waste processing resources of a Rails app just for serving static files. Better tell the webserver to do so.
I fixed this problem by running this command:
RAILS_ENV=production rake assets:precompile
Reference: http://guides.rubyonrails.org/asset_pipeline.html
My environment is Nginx
+ Unicorn
+ Rails4
精彩评论