---
title: "Rails Cache-Control"
description: "若要在 Heroku 上启用静态资源的 cache-control 响应头设置并使用 CDN："
canonical: https://oozou.com/til/rails-cache-control-77
locale: zh-Hans
date: 2020-10-27
topic: ruby-on-rails
---

# Rails Cache-Control

> 若要在 Heroku 上启用静态资源的 cache-control 响应头设置并使用 CDN：

Published: 2020-10-27 · Topic: [ruby on rails](https://oozou.com/zh-Hans/til-topics/ruby-on-rails) ([markdown](https://oozou.com/zh-Hans/til-topics/ruby-on-rails.md))

若要在 Heroku 上启用静态资源的 cache-control 响应头设置并使用 CDN：

```rails
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.public_file_server.headers = {
  'Cache-Control' => 'public, max-age=31536000',
  # You need to enable this (or set it to your domain) for fonts to work
  'Access-Control-Allow-Origin' => '*'
}

if ENV['ASSET_HOST'].present?
  config.action_controller.asset_host = ENV['ASSET_HOST'] # 'https://cdn.example.com'
end
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/zh-Hans/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
