在RAILS 3.1中的js资产上设置Cache-Control标头

时间:2021-10-09 05:55:08

I check the headers on my production server as

我检查生产服务器上的标题为

    curl --head -H "Accept-Encoding: gzip" http://foo.heroku.com/assets/mobile.js

and I get back the following headers which suggest RAILS is not setting the cachecontrol headers.

然后我回到以下标题,建议RAILS不设置cachecontrol标头。

HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Wed, 22 Jun 2011 12:01:55 GMT
Content-Type: application/javascript
Connection: keep-alive
Content-Md5: efb90436a465f8a73efb467109f745f3
Cache-Control: no-cache
Last-Modified: Wed, 22 Jun 2011 11:46:04 GMT
Etag: "efb90436a465f8a73efb467109f745f3"
X-Ua-Compatible: IE=Edge,chrome=1
X-Runtime: 0.001258
X-Content-Digest: 6493f457e9550773761bb1c2c52ec4cb44a19c19
X-Rack-Cache: stale, valid, store
X-Varnish: 164373614
Age: 0
Via: 1.1 varnish
Content-Encoding: gzip

I'd like to get heroku's varnish cache caching the assets and only refreshing on git push. Any ideas on how to get this?

我想让heroku的varnish缓存缓存资产,只在git push上刷新。关于如何获得这个的任何想法?

Brad

2 个解决方案

#1


7  

You have to add this in your environment/production.rb:

您必须在您的环境/ production.rb中添加它:

config.serve_static_assets = true
config.static_cache_control = "public, max-age=172800"

#2


1  

Camille's answer is right for Rails versions before 5.1

Camille的答案适用于5.1之前的Rails版本

However, in Rails 5.1 config.static_cache_control will be deprecated. The code updated with the new available option should be:

但是,在Rails 5.1中,不推荐使用config.static_cache_control。使用新的可用选项更新的代码应为:

config.serve_static_assets = true
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age= 172800' }

#1


7  

You have to add this in your environment/production.rb:

您必须在您的环境/ production.rb中添加它:

config.serve_static_assets = true
config.static_cache_control = "public, max-age=172800"

#2


1  

Camille's answer is right for Rails versions before 5.1

Camille的答案适用于5.1之前的Rails版本

However, in Rails 5.1 config.static_cache_control will be deprecated. The code updated with the new available option should be:

但是,在Rails 5.1中,不推荐使用config.static_cache_control。使用新的可用选项更新的代码应为:

config.serve_static_assets = true
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age= 172800' }