23

I'm running a Rails 3.1.1 application on Heroku Cedar. By default this stack doesn't Gzip and set Expires Headers on assets. There is some doc about that, but it's not very clear : http://devcenter.heroku.com/articles/http-routing

Can somebody give me the piece of code to activate that ?

Thank you very much

Camille
  • 678
  • 6
  • 23

3 Answers3

38

Cedar doesn't use Nginx, so you have to gzip assets yourself with Rack::Deflater, like so :

# config.ru
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run YourApp::Application

Also you can set headers for static files directly in your app :

# config/environments/production.rb
config.static_cache_control = "public, max-age=3600"

Finally you're probably better off setting up Rack::Cache to replace Varnish caching. See this blog post for more infos.

DuoSRX
  • 4,179
  • 3
  • 26
  • 32
  • Here is the response header I get on assets with theses changes: `HTTP/1.1 304 Not Modified Cache-Control: no-cache, private Date: Mon, 17 Oct 2011 11:36:48 GMT Server: thin 1.2.11 codename Bat-Shit Crazy X-Rack-Cache: miss X-Runtime: 0.001256 X-Ua-Compatible: IE=Edge,chrome=1 Connection: keep-alive` – Camille Oct 17 '11 at 11:37
  • In fact, you're right! It's just need to activate `serve_static_assets`using `config.serve_static_assets = true` – Camille Oct 17 '11 at 11:47
  • 2
    This is the place where it's discussed in the heroku dev center that brought me here: http://devcenter.heroku.com/articles/http-routing This answer really helped me out! – pizza247 Feb 04 '12 at 00:25
  • @Camille I want to know how to find out what the expires headers is for a site, example google.com. how can i find that out? – umar May 11 '12 at 10:20
9

Shameless plug - I created a gem which enables compression, but avoids compressing images.

https://github.com/romanbsd/heroku-deflater

Roman
  • 13,100
  • 2
  • 47
  • 63
  • I tried installing your gem but then my compiled assets can't be found. Any help here? – Harry Moreno Feb 06 '14 at 02:28
  • No idea, this is the first time I hear about such thing. – Roman Feb 06 '14 at 05:43
  • oh, sorry it was a wrongly configured staging.rb file. I got my assets served correctly again. But I'm not seeing expires headers in my yslow report. Is it just supposed to 'work' or do I have to do more configuring? – Harry Moreno Feb 06 '14 at 19:10
  • 2
    For assets served by the rails app, you should see Cache-Control header. If it's served by S3 or cloudfront, then you should configure it there. – Roman Feb 09 '14 at 08:06
5

It is important that the middleware be included early, before ActionDispatch::Static

#production.rb
config.middleware.insert_before ActionDispatch::Static, Rack::Deflater

> rake middleware
use Rack::Deflater
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f8e18455e90>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Remotipart::Middleware
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
use Warden::Manager
use Rack::Mongoid::Middleware::IdentityMap
use Rack::Pjax
run MyApp::Application.routes
Peter Ehrlich
  • 6,969
  • 4
  • 49
  • 65