如何在 Heroku Cedar 上的 Rails 3.1.1 应用程序上获取 Gzip 和 Expires 标头?

发布于 2024-12-10 11:16:55 字数 313 浏览 0 评论 0原文

我正在 Heroku Cedar 上运行 Rails 3.1.1 应用程序。默认情况下,此堆栈不会 Gzip 并设置资源上的过期标头。 有一些关于此的文档,但不是很清楚: http://devcenter.heroku.com/articles/http-routing

有人可以给我一段代码来激活它吗?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍风燃霜 2024-12-17 11:16:55

Cedar 不使用 Nginx,因此您必须使用 Rack::Deflater 自己对资源进行 gzip,如下所示:

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

您还可以直接在应用程序中设置静态文件的标头:

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

最后,您最好设置 Rack::Cache 取代 Varnish 缓存。请参阅这篇博文 了解更多信息。

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.

只等公子 2024-12-17 11:16:55

无耻插件 - 我创建了一个可以压缩的 gem,但避免压缩图像。

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

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

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

花心好男孩 2024-12-17 11:16:55

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文