Cloudflare Pages

Cloudflare Hugo

This website moved from GitHub Pages to Cloudflare Pages.

The build did not change: Hugo, Tailwind and Pagefind still run in GitHub Actions. Only the last step, the upload, now points at Cloudflare.

Contents

Pages project

In the Cloudflare dashboard: Workers & Pages -> Create -> Continue to Pages (the cards above it create a Worker, not a Pages project) -> Drag and drop your files.

  • Direct upload, not “Import an existing Git repository”: GitHub Actions keeps building, and Cloudflare needs no access to the private repository.
  • A new project needs a first upload: build locally and drop the public folder:
$ hugo --gc --minify
$ npx pagefind --site public

The site is then live at https://<project>.pages.dev.

Cloudflare API token

My Profile -> API Tokens -> Create Token -> Create Custom Token (“Start from scratch”), with a single permission:

ScopeResourceAccess
AccountCloudflare PagesEdit

Limit it to your own account, leave the zone resources empty, and skip IP filtering (GitHub runners change IP on every run). The token is shown only once.

In GitHub, Settings -> Secrets and variables -> Actions, add two repository secrets: CLOUDFLARE_API_TOKEN and CLOUDFLARE_ACCOUNT_ID (the account ID is on the dashboard’s home page).

GitHub Actions

The GitHub Pages steps (configure-pages, upload-pages-artifact, deploy-pages) go away, together with their pages and id-token permissions. Hugo now takes the baseURL from the config instead of from configure-pages, and the last step uploads public with wrangler:

- name: Build with Hugo
  run: hugo --gc --minify

- name: Run Pagefind
  run: npx pagefind --site public

- name: Deploy to Cloudflare Pages
  uses: cloudflare/wrangler-action@<sha> # v4.1.3
  with:
    apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
    accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
    command: pages deploy public --project-name=<project> --branch=main

--branch=main must match the project’s production branch, or the upload becomes a preview deployment instead of the live site.

DNS

The four GitHub Pages A records go away, and both names point at the project, proxied through Cloudflare:

NameTypeContentProxied
althaser.meCNAME<project>.pages.devyes
www.althaser.meCNAME<project>.pages.devyes

A CNAME at the zone apex is normally not allowed; Cloudflare flattens it into the edge IPs, so the apex keeps its other records (TXT, CAA). Here the records live in OpenTofu, as cloudflare_dns_record resources:

resource "cloudflare_dns_record" "website" {
  zone_id = var.zone_id
  name    = "althaser.me"
  type    = "CNAME"
  content = "<project>.pages.dev"
  ttl     = 1
  proxied = true
  comment = "cloudflare pages"
}

If the apply fails because the new CNAME clashes with the old A records, run it again: by then the A records are gone.

Until the next step the site answers 522: the DNS already reaches Cloudflare, but the project does not know the domain yet.

Custom domains

In the project, Custom domains -> Set up a custom domain, once for althaser.me and once for www.althaser.me. The records already point at the project, so it only asks to activate them. Each goes from Verifying to Active in a few minutes, once its certificate is issued.

Activating the domains replaces the two CNAME records with new ones, same content but new IDs. OpenTofu then reports them as Resource not found and plans to create them again, which would fail since they exist. Import the new IDs instead, under new keys: an import block is ignored while the old address is still in the state.

dig outputs

$ dig althaser.me +short
104.21.85.230
172.67.211.226

$ dig althaser.me +nostats +nocomments +nocmd
;althaser.me.			IN	A
althaser.me.		300	IN	A	172.67.211.226
althaser.me.		300	IN	A	104.21.85.230

$ dig www.althaser.me +nostats +nocomments +nocmd
;www.althaser.me.		IN	A
www.althaser.me.	300	IN	A	172.67.211.226
www.althaser.me.	300	IN	A	104.21.85.230

$ dig AAAA althaser.me +short
2606:4700:3033::6815:55e6
2606:4700:3035::ac43:d3e2

The GitHub Pages A records (185.199.108.153 to 185.199.111.153) are gone. Both names now answer with Cloudflare’s edge addresses, IPv6 included, and since they are proxied the CNAME to <project>.pages.dev never shows up in the answer.

Checks

$ curl -sI https://althaser.me | grep -i '^HTTP\|^server'
HTTP/2 200
server: cloudflare

$ curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' http://althaser.me/
301 -> https://althaser.me/

$ echo | openssl s_client -connect althaser.me:443 -servername althaser.me 2>/dev/null | openssl x509 -noout -issuer
issuer=C=US, O=Google Trust Services, CN=WE1

The certificate now comes from Google Trust Services instead of Let’s Encrypt. With a CAA record on the domain this still works: while Universal SSL is on, Cloudflare adds its own certificate authorities to the CAA answer.

$ dig +short CAA althaser.me
0 issue "comodoca.com"
0 issue "digicert.com; cansignhttpexchanges=yes"
0 issue "letsencrypt.org"
0 issue "pki.goog; cansignhttpexchanges=yes"
0 issue "ssl.com"
0 issuewild "comodoca.com"
0 issuewild "digicert.com; cansignhttpexchanges=yes"
0 issuewild "letsencrypt.org"
0 issuewild "pki.goog; cansignhttpexchanges=yes"
0 issuewild "ssl.com"

letsencrypt.org is the only one in the zone’s own record; the others are the ones Cloudflare adds, pki.goog being Google Trust Services.

Differences from GitHub Pages

  • www: GitHub redirected www to the apex by itself; Cloudflare Pages serves the site on both names. A Single Redirect rule brings the redirect back (see below).
  • Headers: GitHub Pages sets no custom response headers. Cloudflare Pages reads a _headers file from the site root (static/_headers in Hugo), so security headers are now possible (see below).
  • 404: both serve the site’s 404.html for missing pages.

www redirect

A Single Redirect rule on the zone sends every www request to the same path on the apex, with a 301 and the query string kept. In OpenTofu it is a cloudflare_ruleset in the http_request_dynamic_redirect phase (a zone has one ruleset per phase, so every redirect goes in this one):

resource "cloudflare_ruleset" "redirects" {
  zone_id = var.zone_id
  name    = "redirects"
  kind    = "zone"
  phase   = "http_request_dynamic_redirect"

  rules = [{
    ref         = "www-to-apex"
    description = "www-to-apex"
    expression  = "(http.host eq \"www.althaser.me\")"
    action      = "redirect"
    action_parameters = {
      from_value = {
        status_code           = 301
        preserve_query_string = true
        target_url = {
          expression = "concat(\"https://althaser.me\", http.request.uri.path)"
        }
      }
    }
  }]
}

The token that runs OpenTofu needs Zone -> Single Redirect -> Edit on the zone, next to DNS -> Edit; without it the API answers 10000 Authentication error. The rule only applies to proxied records, one more reason to keep them proxied.

$ curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' 'https://www.althaser.me/blog/?x=1'
301 -> https://althaser.me/blog/?x=1

$ curl -s -o /dev/null -w '%{http_code} -> %{redirect_url}\n' http://www.althaser.me/visuals/
301 -> https://althaser.me/visuals/

Security headers

static/_headers applies a few headers to every page:

/*
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  X-Frame-Options: SAMEORIGIN
  Permissions-Policy: camera=(), microphone=(), geolocation=()
  Strict-Transport-Security: max-age=31536000
  • X-Frame-Options is SAMEORIGIN, not DENY, since the site embeds its own interactive SVGs.
  • Strict-Transport-Security has no includeSubDomains or preload, so it only covers this name and can be rolled back.
  • No Content-Security-Policy yet: Pagefind needs WebAssembly and the analytics script loads from another domain, so it should start as Content-Security-Policy-Report-Only before being enforced.
$ curl -sI https://althaser.me | grep -i 'x-content\|referrer\|x-frame\|permissions\|strict-transport'
x-frame-options: SAMEORIGIN
strict-transport-security: max-age=31536000
permissions-policy: camera=(), microphone=(), geolocation=()
referrer-policy: strict-origin-when-cross-origin
x-content-type-options: nosniff

X-Content-Type-Options and Referrer-Policy already come by default from Cloudflare Pages; they stay in the file so it lists everything the site sends.

References