Legendary Arena Lab

Cloudflare TLS

web

Migration in progress. api.legendary-arena.com and PostgreSQL are moving from Render to a self-hosted DigitalOcean Ubuntu host behind Cloudflare. Render remains rollback-ready until decommission phase.

Cloudflare TLS

Configure origin TLS and edge posture so api traffic is proxied through Cloudflare with strict certificate validation.

Summary

This page owns infra/scripts/21-cloudflare-tls.ps1 .

The script performs host-side setup:

  • installs Cloudflare Origin Certificate and key at the origin
  • rewrites Nginx site config for HTTPS proxying
  • validates and reloads Nginx
  • fetches Cloudflare IP ranges and restricts UFW 443 ingress

Dashboard-side settings remain manual and required:

  • DNS record for api is proxied
  • Full (strict) for the api hostnames through a Configuration Rule (Rules → Configuration Rules → Origin TLS strict — api hosts, SSL: Strict). The zone itself stays on Full, so the other sites on legendary-arena.com are unaffected.

Prerequisites

  • Nginx baseline configured from nginx reverse proxy step
  • UFW enabled
  • Cloudflare Origin Certificate and private key issued for api domain

Origin certificate

Create it in the zone under SSL/TLS → Origin Server → Create Certificate: RSA 2048, 15 years, hostnames api.legendary-arena.com and api-next.legendary-arena.com only, with no wildcard. The private key is shown once. The operator saves both files and installs them on the host personally, so the key goes nowhere else:

scp -i $HOME\.ssh\id_ed25519 "<folder>\origin.crt" "<folder>\origin.key" operator@<droplet-ip>:/tmp/
ssh -i $HOME\.ssh\id_ed25519 operator@<droplet-ip> "sudo install -d -m 755 /etc/ssl/cloudflare && sudo install -m 644 /tmp/origin.crt /etc/ssl/cloudflare/origin.crt && sudo install -m 600 /tmp/origin.key /etc/ssl/cloudflare/origin.key && rm /tmp/origin.crt /tmp/origin.key"

Check that the key matches the certificate without printing either: the two modulus hashes must be equal.

openssl x509 -in /etc/ssl/cloudflare/origin.crt -noout -modulus | sha256sum
sudo openssl rsa -in /etc/ssl/cloudflare/origin.key -noout -modulus | sha256sum

Execute

With the files pre-installed (preferred), DOMAIN takes a space-separated list for the Nginx server_name:

sudo DOMAIN="api.legendary-arena.com api-next.legendary-arena.com" UPSTREAM_PORT=3000 \
  pwsh -File infra/scripts/21-cloudflare-tls.ps1

Alternatively, set cert and key as environment variables and run:

sudo DOMAIN=api.legendary-arena.com UPSTREAM_PORT=3000 \
  CF_ORIGIN_CERT="$(cat origin.crt)" \
  CF_ORIGIN_KEY="$(cat origin.key)" \
pwsh -File infra/scripts/21-cloudflare-tls.ps1

If cert files are already installed at default paths, run without the cert variables.

Verify

sudo nginx -t
sudo systemctl status nginx --no-pager
sudo ufw status numbered

Before cutover, test through a throwaway proxied hostname (api-next) pointed at the droplet, so production api. is never involved:

# 1. Health through Cloudflare with strict TLS: expect 200
curl -sS -o /dev/null -w '%{http_code}\n' https://api-next.legendary-arena.com/health

# 2. Socket.IO polling handshake: expect 0{"sid":...}
curl -sS "https://api-next.legendary-arena.com/socket.io/?EIO=4&transport=polling"

# 3. WebSocket upgrade: expect 101 (curl then waits on the open socket; --max-time ends it)
curl -sS --http1.1 --max-time 8 -o /dev/null -w '%{http_code}\n' \
  -H 'Connection: Upgrade' -H 'Upgrade: websocket' \
  -H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==' \
  "https://api-next.legendary-arena.com/socket.io/?EIO=4&transport=websocket"

# 4. Droplet IP directly on 443: expect a timeout (UFW admits only Cloudflare)
curl -sk --max-time 8 --resolve api-next.legendary-arena.com:443:<droplet-ip> \
  https://api-next.legendary-arena.com/health

A 526 from Cloudflare means strict validation rejected the origin certificate. A 521 means nothing is listening on 443 yet.

Rehearsed 2026-10-11: all four pass on the rehearsal droplet.

Cutover and rollback

  • Cutover: add api.legendary-arena.com to the Configuration Rule. Then replace the api. record (today a DNS-only CNAME → legendary-arena-server.onrender.com) with an A record → droplet IP, proxied.
  • Rollback: restore exactly that CNAME, DNS-only. Render runs its own edge and certificate for api..
  • After cutover, delete the api-next record (and drop it from the rule) once it is no longer needed.

Keeping the Cloudflare ranges current

21-cloudflare-tls.ps1 fetches Cloudflare’s IP ranges once. Cloudflare changes the list now and then. A new range missing from UFW means some visitors’ requests never reach the origin, and a retired range left in UFW stays open to whoever holds it next. infra/scripts/22-cloudflare-ufw-refresh.ps1 keeps them in step on a weekly la-cf-ufw-refresh.timer:

  • it fetches ips-v4 and ips-v6 and validates both before touching UFW: every line must be a CIDR, and each list must have at least 10 (IPv4) / 5 (IPv6) entries. A failed or garbled fetch changes nothing and exits 1.
  • it adds missing ranges as allow proto tcp from <cidr> to any port 443.
  • it removes any such 443 rule whose source is no longer published. On this host 443 exists only for Cloudflare, so every from-CIDR 443 rule counts as managed.
  • it skips a host where UFW is not active, as 21 does.

A failed run leaves the unit in a failed state, and 60-monitor.ps1 alerts on that (see Monitoring ). Install once:

sudo pwsh -File infra/scripts/22-cloudflare-ufw-refresh.ps1            # one refresh now
sudo pwsh -File infra/scripts/22-cloudflare-ufw-refresh.ps1 -Install   # weekly timer
systemctl list-timers la-cf-ufw-refresh.timer --no-pager

Important gotcha

Cloudflare Origin Certificates are trusted only through Cloudflare proxy. If you grey-cloud the DNS record, direct browser trust will fail by design.

References

  • Plan section 4.3 cloudflare-tls page stub
  • Plan section 5.1 TLS and edge posture checklist