ruby-on-rails - 如何使用 Heroku 和 Cloudfront 为 Rails 激活页面缓存?

标签 ruby-on-rails ruby amazon-web-services heroku browser-cache

网站分析器说我没有激活页面缓存,https://tools.pingdom.com/#!/bDj23F/www.anthonygalli.com .不过,我还是按照我能动手的每一步去做。我正在使用 rails "4.2.7.1"

在 View 中

<%= render :partial => @posts, cached: true %>

在 Controller 中

fresh_when last_modified: @posts.created_at.utc, etag: @posts

生产中.rb

config.action_controller.perform_caching = true
config.action_controller.asset_host = 'd37p52igaahgm9.cloudfront.net'

更新

enter image description here

最佳答案

您可以在云端分配中做的一件事是添加一个自定义 header 参数。在这里,GoogleAmazon不完全同意,因为谷歌推荐使用Expires header,亚马逊推荐使用Cache-Control max-age 参数/值,所以你必须选择一个,因为亚马逊会如果指定了两者,则仅处理 Cache-Control

We recommend that you use the Cache-Control max-age directive instead of the Expires header field to control object caching. If you specify values both for Cache-Control max-age and for Expires, CloudFront uses only the value of Cache-Control max-age.

如果您的云端分配来自 S3 存储桶,您可以 Add Headers to Your Objects Using the Amazon S3 Console

  • 从 aws s3 控制台打开 s3 对象的属性
  • 在右 Pane 中,展开元数据。
  • 点击添加更多元数据。
  • 在 key 列表中,单击缓存控制或过期(视情况而定)。
  • 在“值”字段中,输入适用的值:

    • 对于缓存控制字段,输入:

      max-age=number of seconds that you want objects to stay in a CloudFront edge cache
      
    • 对于“过期”字段,以 HTML 格式输入日期和时间。

  • 点击保存。

见下文

enter image description here

关于ruby-on-rails - 如何使用 Heroku 和 Cloudfront 为 Rails 激活页面缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39588396/

相关文章:

mysql - 目录 : how do I test my app against multiple databases?

ruby-on-rails - Rails-使用camelize将模型序列化为JSON

ruby - 如何从 Sinatra 进行 Github 风格的 Markdown 渲染?

mysql - 使用 Amazon Web Services(EC2 等)托管 CakePHP 应用程序?

node.js - 将 yeoman node.js 应用程序部署到 Elastic Beanstalk

windows - 无法使用 winrm 引导

ruby-on-rails - Rails 3 分页,will_paginate 与 Kaminari

ruby-on-rails - 图像的动态CSS

ruby - 安装 SASS gem 时出错 - 没有权限

ruby-on-rails - 提示需要散列的事件资源