How to get Gzip and Expires Header on a Rails 3.1.1 app on Heroku Cedar?
Asked Answered
P

3

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

Presumptive answered 17/10, 2011 at 10:49 Comment(0)
U
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.

Unequaled answered 17/10, 2011 at 11:17 Comment(4)
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-alivePresumptive
In fact, you're right! It's just need to activate serve_static_assetsusing config.serve_static_assets = truePresumptive
This is the place where it's discussed in the heroku dev center that brought me here: devcenter.heroku.com/articles/http-routing This answer really helped me out!Armes
@Presumptive 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?Howitzer
A
9

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

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

Arliearliene answered 16/1, 2013 at 10:59 Comment(4)
I tried installing your gem but then my compiled assets can't be found. Any help here?Soursop
No idea, this is the first time I hear about such thing.Arliearliene
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?Soursop
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.Arliearliene
S
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
Submicroscopic answered 25/10, 2012 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.