Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Rails serve static content out of public?

How much ruby code deals with serving static content out of public? Does it pass through the rails app at all? Does it use Rack::Static?

like image 763
John Bachir Avatar asked Mar 30 '12 15:03

John Bachir


2 Answers

Rails doesn't use Rack::Static, it has its own version, ActionDispatch::Static. You should see it if you run rake middleware.

This is only added to the Rails middleware stack if config.serve_static_assets is true. This setting defaults to true, but the default generated config/environments/production.rb turns if off.

The idea is that during development you have a simple single process that you can run and check everything is working and where performance isn't an issue, but when you deploy to production you configure your webserver (usually Apache or Nginx) to serve the static files as it is much better at that than Ruby.

If you use Heroku, their latest Cedar stack doesn't use a separate webserver for static files, so as part of the deploy process they inject a Rails plugin to serve static assets. All this plugin does is set serve_static_assets to true.

like image 175
matt Avatar answered Nov 19 '22 16:11

matt


It depends. If you're using something like phusion passenger to run your app, its default behavior is to use Apache directly and skip rails for any static content. If you're using webrick (rails server) it's going to use a lot more rails/rack code to do the static serving.

A note though, if you're using apache/nginx with passenger: in production mode, since the default is to let apache serve all static content, you will need to run rake assets:precompile on the application prior to launching it in apache, or the static content doesn't get put where it needs to be for apache to get it quickly, and in a prod-ready form.

like image 6
waxspin Avatar answered Nov 19 '22 15:11

waxspin