0

我一直在尝试摆脱浏览器为我们的 Rails 应用程序发出的重新验证请求。我打开了资产摘要:

      config.assets.digest = true

并希望304的消失。但他们没有(资产是 md5'ed):

Started GET "/assets/bootstrap/bootstrap-4379dca12de4846777a274a301167a41.css" for 192.168.56.1 at Sun Mar 03 00:34:22 +0100 2013
[INFO  pid: 3967: 13-03-03 00:34:22 ] Served asset /bootstrap/bootstrap-4379dca12de4846777a274a301167a41.css - 304 Not Modified (5ms)

供参考 - 这是页面中的原始 html:

  <link href="/assets/bootstrap/bootstrap-4379dca12de4846777a274a301167a41.css" media="screen" rel="stylesheet" type="text/css" />

我使用 curl 检查了缓存头 - 它们看起来不错:

< Content-Type: text/css
< Connection: keep-alive
< Status: 200
< X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.11
< ETag: "4379dca12de4846777a274a301167a41"
< X-UA-Compatible: IE=Edge
< Last-Modified: Tue, 26 Feb 2013 08:03:21 GMT
< X-Rack-Cache: miss, store
< X-Content-Digest: 97d1f611b557f55fa917899bca0ef9b674d65a6a
< X-Runtime: 0.023510
< Date: Sat, 02 Mar 2013 23:43:37 GMT
< Content-Length: 141338
< Cache-Control: public, max-age=31536000
< Age: 0
< Server: nginx/1.0.10 + Phusion Passenger 3.0.11 (mod_rails/mod_rack)

如果我直接从浏览器加载这个 css URL - 那么浏览器似乎确实缓存了它。(刷新不会在 rails 日志上生成 GET)。但是,当我加载我的应用程序页面时 - 它总是发出重新验证,导致 304。

我玩弄了这些设置(真/假,存在/不存在)(几乎尝试了所有组合)(并且总是删除 tmp/cache 只是为了安全起见):

config.assets.compile = true
config.static_cache_control = "public, max-age=30758400"
config.serve_static_assets = true

但没有任何帮助。我在某处读到“Etag”标头是问题所在 - 但我无法弄清楚如何关闭 Etag 标头(无论我做什么 - 始终返回 etag 标头)。(而且我不明白为什么官方食谱不起作用)。

这是在开发模式 - 但我已经改变了配置设置,如上所示。没有预编译。

非常感谢有关如何关闭重新验证的任何帮助。

4

1 回答 1

2

Rails 可能没有添加这些标头……nginx 是。查看 Rails 指南部分(向下滚动到 4.1.1):

http://edgeguides.rubyonrails.org/asset_pipeline.html#precompiling-assets

特别是“For nginx”部分:

location ~ ^/assets/ {
  expires 1y;
  add_header Cache-Control public;

  add_header ETag "";
  break;
}
于 2013-03-03T07:02:06.580 回答