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?
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.
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.
Related
I am running the same rails 5.1.5 app in development and production. Everything works as expected in the development environment. In production, I could not serve the static assets in the public folder initially. I set the config.public_file_server.enabled = true in production.rb. However, since every request on my page is behind an authorization (as a :before_action in the application controller), the .js file that sets the authorization cookie is also not served without authorization and my service is caught in a loop. This does not happen in the development mode, and the public assets are not requested through rails routes. I need guidance on how the serving of public assets is different in development and production mode for a rails application.
Well I believe this could be due to a problem in your production configuration. For an example in Production you don't let static resources to be served by the Rails process. You should instead set static assets to be served by the Web server. You cam simply do this by adding the following configuration to you Apache or Nginx virtual files.
DocumentRoot /home/xxx/test/public
root /var/www/phindee/current/public;
I have an action, that generates a PDF files and save it in the /public/output.pdf.
When I set
config.serve_static_assets = false
this file can't be found.
What's wrong ?
From the documentation:
"config.serve_static_assets configures Rails itself to serve static
assets. Defaults to true, but in the production environment is turned
off as the server software (e.g. Nginx or Apache) used to run the
application should serve static assets instead. Unlike the default
setting set this to true when running (absolutely not recommended!) or
testing your app in production mode using WEBrick. Otherwise you won´t
be able use page caching and requests for files that exist regularly
under the public directory will anyway hit your Rails app."
Which means that if you set that to false Rails will not serve any assets from your public folder as it is assumed that a front-end web server (apache/nginx) will handle it. This lessons the load on Rails as the front-end server is much, much more efficient at serving files directly.
After testing, I came to this conclusion:
1) when using the command
rails s -e production
Rails will only serve the statics files. Any other file created after you compile your assets will not be found.
To handle this, you need to execute your application under a web server like Apache, Nginx or other. These web servers will serve this files for you.
This looks to be obvious, but not for a beginner.
I have a Sinatra app in a Rails app that serves a static asset from a directory. The app is mounted in the Rails routes like this:
mount BeanstalkdView::Server, :at => "/beanstalk"
When I run this locally it works fine, using Thin, but when I run it on my test server (Nginx/Passenger) the static assets behave strange. A request to a static file return 200 OK, but there is no content.
I tell Sinatra where my static files are via set :public_folder, "#{root}/resources" and in the template I load the static files, e.g. a CSS file with #{request.env['SCRIPT_NAME']}/css/file.css. I verified that both paths are correct.
That happens due to ::Rack::Sendfile middleware which is enabled by default in Rails 3.0.x in production (disabled by default in any environment since 3.1.x).
This middleware is really simple but yet powerful. When you pass ::Rack::File or ::Sinatra::StaticFile (or any other object) which respond to :path, this middlware adds X-SendFile (for Apache) or X-SendFile-Redirect (for NGinx) and does not sends actual body. So that Apache or NGinx will handle real file delivery. It's a good and most efficient way to serve static assets in production, however you may disable this middleware (if you don't want to mess with your Nginx/Apache config). Find and comment following config option in your config/environments/production.rb file:
config.action_dispatch.x_sendfile_header
this config option is used to instruct Sendfile middleware which header to set (do not do anything if not specified).
Aleksey V's answer helped me a lot. In the end I fixed this using the proper setting for Nginx in production.rb:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Make sure you restart your Rails app, Nginx and do a hard refresh in your browser to get the files.
For more info check out: http://jimneath.org/2011/04/05/rails-3-nginx-and-send_file.html.
I am running a Rails 3.0.9 app on Heroku's Cedar stack and have S3 serving static assets. In my production.rb file, there was a config set to:
config.serve_static_assets = false
If I change this to true, will it serve cached content quicker, or should I leave it as is?
The answer from Rafal is not strictly correct as essentially it comes down to what stack and what version of Rails you choose to run in your application.
With Rails 3.0 on the Bamboo stack there is a Varnish cache which sits in front of the Thin processes that Heroku run. This caches any static assets and returns them without hitting your application.
With Rails 3.0 on the Cedar stack there is no Varnish cache. Therefore all requests will be hitting your Rails process regardless of whether they are static or not.
With Rails 3.1 which should be on Cedar Heroku will try to run a rake assets:precompile as part of the slug compilation process. If this fails for any reason it will inject some code into your slug meaning that static assets are compiled and served at at run time.
There is a Rails 3.1 document on the dev center which is particularly useful regarding this: http://devcenter.heroku.com/articles/cdn-asset-host-rails31
So, if you're looking for the correct setting bear this in mind. However, before you do that, notice that Heroku will alter this setting as they see fit when you do your deploy, so any setting that you do put in will be overwritten anyway during the slug compilation. Therefore it doesn't really matter what you put in here.
(and for the record, Heroku uses Nginx, you just don't tend to see it)
From rails guide:
config.serve_static_assets configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won´t be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
Hope this helps.
I'm learning Ruby on Rails. At the moment I'm just running my site locally with rails server in the OS X Terminal. What changes when a Rails site is run on a production box?
Is the site still started with rails server?
Any differences with how the db is setup?
Note: I'm running Rails 3.
A rails app can be run in production calling rails server -e production, although 99% of the time you'll be serving on something like passenger or thin instead of WEBrick, which means there's a different command to start the server. (thin start -e production for instance)
This is a complicated question, but the best place to start learning about the differences would be to look at the specific environment.rb files. When rails boots up it starts with the environment file that matches the called environment, ie if you start it in development it begins by loading your development.rb file, or if you're in production it will load the production.rb file. The differences in environments are mostly the result of these differences in the various environment config files.
Basically if a Rails 3.1 app is in production mode, then by default it is not going to be compiling assets on the fly, and a lot of caching will be going on that isn't happening in development. Also, when you get error messages they will be logged but not rendered to the user, instead the static error page from your public directory will be used.
To get more insight into this, I would suggest reading the relevant rails guides:
Rails Initialization Guide: http://guides.rubyonrails.org/initialization.html
Rails Configuration Guide: http://guides.rubyonrails.org/configuring.html
There are two contexts you can use the word "production" here. One of them is running the server in production mode. You can do this locally by,
RAILS_ENV=production ./script/server
The configuration for this is picked up from config/environments/production.rb. Try comparing this file with config/environments/development.rb. There are only subtle differences like caching classes. Development mode makes it easier so that it will respond to any changes you make instantly. Plus there are two different databases (by default) will be used namely yourproject_development and yourproject_production if you choose to run your server in either of these modes.
On the other hand, rails deployment to a production box is something different. You will need to pick your server carefully. You may have to deal with a deployment script may be capistrano. You may also need a load balancer such as netgear. The database also may require a deep consideration like size expectation, master/slave clustering etc.,
Note: I have never used Rails 3. This answer is biased towards 2.3.x.