skip to content

In a Rails 8.1 app's production.rb, what do `config.public_file_server.headers` and `config.asset_host` each control once a CDN fronts the pet-insurance quote site?

level: middleimportance: should knowfreq 34%

answer

  1. ActionDispatch::Static serves public/
  2. public, max-age of 1.year.to_i
  3. applies to every file in public/
  4. asset_host rewrites asset helper URLs
  5. proc, %d, or plain host

basics

~20 s

public_file_server.headers sets the response headers on every file Rails serves from public/, generated as a one-year public Cache-Control. asset_host makes asset helpers emit URLs on another host, such as a CDN that pulls those files from the app.

solid answer

~40 s

`config.public_file_server` configures `ActionDispatch::Static`, the middleware that serves files from `public/`; it is enabled by default and the generated `production.rb` sets `headers = { "cache-control" => "public, max-age=#{1.year.to_i}" }`. That is right for digest-stamped files in `public/assets`, whose names change with their content, but the hash applies to every file `Static` serves, so `robots.txt` or `icon.png` is also cached for a year. `config.asset_host` changes the URLs that helpers such as `asset_path`, `image_tag("logo.png")`, `stylesheet_link_tag` and `javascript_include_tag` build: a host like `"cdn.quotes.example"`, a `%d` pattern, or a proc. The browser then asks the CDN, which pulls `/assets/...` from the app once and keeps it as long as the headers allow. Setting it from `ENV["CDN_HOST"]` leaves staging on relative URLs when the variable is unset.

code

ruby · 12 lines
ruby
# config/environments/production.rb (pet-insurance quote app)
config.public_file_server.headers = {
  "cache-control" => "public, max-age=#{1.year.to_i}"
}

# nil when CDN_HOST is unset: helpers keep relative URLs
config.asset_host = ENV["CDN_HOST"]

# or choose per request, e.g. no CDN for the internal admin host
# config.asset_host = ->(source, request) {
#   request&.host == "admin.quotes.example" ? nil : "https://cdn.quotes.example"
# }

go deeper

for a junior

Know that assets in public/assets have digests in their names and that the generated header lets browsers cache them for a year.

for a middle

Explain that the header covers every file ActionDispatch::Static serves, and how asset_host values, plain, %d or proc, change helper URLs.

for a senior

Plan the CDN rollout: origin pulls, exemptions or purges for non-digested files, staging without a CDN, and which URLs asset_host does not touch.

for a principal

Decide what the CDN fronts, assets only or uploads and pages too, weighing purge discipline and cost against latency gains.

## Two settings, two directions When a CDN sits in front of the pet-insurance quote app, two lines in `production.rb` decide how assets flow: - **`config.public_file_server`** governs responses the app sends for files in `public/`: the CDN's origin requests, or browsers when there is no CDN. - **`config.asset_host`** governs the URLs the app writes into HTML, which decide whether browsers ask the CDN or the app. ## `public_file_server`: headers on every file in `public/` `config.public_file_server.enabled` defaults to `true`, which puts `ActionDispatch::Static` into the middleware stack. For GET and HEAD requests it looks for a matching file under `public/` and serves it through `Rack::Files`, merging in `config.public_file_server.headers`. Rails 8.1 generates: ```ruby # Cache assets for far-future expiry since they are all digest stamped. config.public_file_server.headers = { "cache-control" => "public, max-age=#{1.year.to_i}" } ``` Points interviewers probe: - The comment holds for **`public/assets`**, where precompiled files carry a digest in their name; a changed file gets a new name, so caching the old one forever is safe. - The header is **one hash for every file** `Static` serves. `public/robots.txt`, `public/icon.png` and `public/icon.svg` keep their names when edited, yet browsers and the CDN may keep the old copy for a year. - `Static` also serves precompressed `.br` or `.gz` siblings when they exist, adding `Vary: accept-encoding`. - Set `enabled = false` only when another server, such as nginx, serves `public/` directly. Fixes for the non-digested files: serve them from a controller action with its own cache headers, add a CDN rule with a short TTL for those paths, or purge them on deploy. ## `asset_host`: where helpers point browsers `config.asset_host` (shorthand that fills `config.action_controller.asset_host` and, unless set separately, `config.action_mailer.asset_host`) is read by `compute_asset_host` whenever a view builds an asset URL: | Value | URL for `asset_path("logo.png")` | |---|---| | `nil` (default) | `/assets/logo-<digest>.png`, relative to the app | | `"cdn.quotes.example"` | `https://cdn.quotes.example/assets/...`, with the scheme taken from the current request | | `"https://cdn.quotes.example"` | that URL as written | | `"assets%d.quotes.example"` | one of `assets0` to `assets3`, chosen by a CRC32 of the source path | | a proc taking `(source)` or `(source, request)` | whatever the proc returns | It affects asset helpers that resolve a **string source**: `asset_path` and `asset_url`, `image_tag("logo.png")`, `stylesheet_link_tag`, `javascript_include_tag`. It does **not** move Active Storage URLs: `image_tag(policy.pet_photo)` resolves through `polymorphic_url` and the app's routes. ## How a request flows with a CDN 1. The app renders `<link href="https://cdn.quotes.example/assets/application-<digest>.css">`. 2. The browser asks the CDN; on a miss, the CDN requests `/assets/application-<digest>.css` from the app (the origin). 3. `ActionDispatch::Static` serves the file with `cache-control: public, max-age=...` for one year. 4. The CDN stores it and answers later requests itself; a new deploy produces a new digest, so the HTML points at a new URL. ## Checking it from the command line Two quick requests confirm the setup before the CDN goes live: 1. `curl -I https://quotes.example/assets/application-<digest>.css` should show `cache-control: public, max-age=` with the one-year value, proving the origin sends what the CDN needs. 2. Viewing the page source should show asset URLs on `cdn.quotes.example`; if they are still relative, `config.asset_host` is unset in the running environment. A response for `robots.txt` with the same one-year header is the reminder that the hash is global. ## Staging and the pet-insurance setup - Read the host from the environment: `config.asset_host = ENV["CDN_HOST"]`. On a staging box without a CDN the variable is unset, the value is `nil`, and URLs stay relative. - Give the host a scheme when no request is available to borrow one from; a scheme-less host there becomes a protocol-relative `//host` URL. - Never point a staging CDN at the production origin, or staging pages load production's assets. - After changing `robots.txt` or the app icon, expect stale copies until the far-future header expires unless you exempted or purged them.

  • The quote app's SEO team edited `public/robots.txt`, but crawlers keep receiving the old file. Why, given the generated production.rb?
    `config.public_file_server.headers` applies its one-year `public` Cache-Control to every file `ActionDispatch::Static` serves, not only digested assets. `robots.txt` keeps its name, so the CDN and clients may keep the cached copy for a year. Purge it at the CDN, and serve it from a controller action or exempt it with a CDN rule so it gets a short lifetime.
  • Does setting `config.asset_host` make uploaded pet photos load from the CDN?
    No. `asset_host` affects asset helpers given a string path, such as `image_tag("logo.png")`. An Active Storage attachment passed to `image_tag` resolves through `polymorphic_url` and the app's routes, so photos keep their app or storage-service URLs unless you put a CDN in front of those routes separately.

  • The one-year public header applies only to files under public/assets
  • config.asset_host uploads precompiled assets to the CDN during deploy
  • Setting asset_host also moves Active Storage attachment URLs to the CDN
  • With a CDN in front, set config.public_file_server.enabled to false
  • asset_host needs a full https:// URL, a bare host is rejected