Two jobs: DNS and proxy
Builds on22. DNS33. Reverse proxies and CDNs
Cloudflare does two separate jobs for Kadé, and most confusion comes from mixing them up:
| # | Job | What it means | Chapter link |
|---|---|---|---|
| 1 | Authoritative DNS for kade.lk | Cloudflare answers "what is the IP of api.kade.lk?". This replaces Cloud DNS's public zone | 8 |
| 2 | Reverse proxy for chosen hostnames | For "proxied" records, Cloudflare answers with its own IPs, so shoppers connect to Cloudflare, which then connects to the GCP load balancer | 11, 12 |
Cloudflare is a proxy in exactly the chapter 11 sense: it ends the shopper's connection at its nearest edge location and opens a new one to Kadé's load balancer. Everything else in this chapter follows from that: two TLS connections, a second set of forwarding headers, and the need to stop people going around it.
Moving kade.lk's DNS to Cloudflare
Making Cloudflare authoritative means changing the domain's name servers at the registrar. The order matters, mainly because of DNSSEC (chapter 8 section 09): if the registrar still holds a DS record for Cloud DNS's signing keys when the name servers change, resolvers that check DNSSEC will reject every answer, and the whole domain stops resolving.
Before the switch
- 1.Add kade.lk to CloudflareCloudflare scans existing records; compare every record with Cloud DNScheck before moving onrecord lists match
- 2.Set records to grey firstEverything DNS only, so the switch changes nobody's path yetcheck before moving onall records grey
- 3.Turn off DNSSEC at the old sideRemove the DS record at the .lk registrar; wait for its TTL (often 1-2 days)check before moving onno DS at the registrar
The switch
- 4.Change name servers at the registrarPoint kade.lk at the two Cloudflare name serverscheck before moving ondig NS kade.lk shows Cloudflare
- 5.Turn on DNSSEC in CloudflareAdd Cloudflare's new DS record at the registrarcheck before moving ondig +dnssec shows the ad flag
After
- 6.Set the mode, then go orangeFull (strict) first, then switch kade.lk, www, api to orange one at a timecheck before moving onsite works through Cloudflare
- 7.Lock the originCloud Armor allows only Cloudflare + secret header (section 06)check before moving ondirect IP returns 403
- 8.Delete the Cloud DNS public zoneOnly after the old name servers have aged out of caches (a few days)check before moving onkade-lk gone; kade-internal kept
Going grey first separates the two risky changes: a DNS provider change (steps 3 to 5) and a traffic path change (step 6). If the site breaks after step 4, it is DNS; after step 6, it is the proxy or TLS mode.
Orange or grey: a choice per record
Every DNS record in Cloudflare has a proxy setting, shown as a cloud icon:
Orange: proxied
Cloudflare answers DNS questions with its own addresses. Visitors connect to Cloudflare, which applies its WAF, DDoS protection and cache, then connects to your origin. The origin's real IP is never published.
Grey: DNS only
Cloudflare is just the DNS server. It answers with the real address in the record, and visitors connect straight to it. No WAF, no cache, no hiding the origin.
Each tab is one of Kadé's records. The "what if" flips api.kade.lk between orange and grey.
Protected: every visitor passes Cloudflare; the origin IP stays out of DNS
Cloudflare answers with its own address. The shopper connects to Cloudflare's edge, which applies the WAF and cache, then connects to 34.120.88.10 itself. The origin IP never appears in DNS.
Show as config
api.kade.lk A 34.120.88.10 proxied (orange) dig +short api.kade.lk → 104.21.48.12, 172.67.140.9 (Cloudflare) grey: → 34.120.88.10 (origin)
| # | Record | Type and value | Proxy | Why |
|---|---|---|---|---|
| 1 | kade.lk | A → 34.120.88.10 | orange | Shoppers type the bare domain too. If it were grey, those visits would skip Cloudflare, and the origin lock (section 06) would block them |
| 2 | www.kade.lk | A → 34.120.88.10 | orange | The website |
| 3 | api.kade.lk | A → 34.120.88.10 | orange | The app and website call it |
| 4 | status.kade.lk | CNAME → a status page provider | grey | Not Kadé's server; the provider handles its own TLS |
| 5 | kade.lk MX | → the mail provider | DNS only | Mail is not HTTP. MX and TXT records cannot be proxied |
| 6 | _acme-challenge.kade.lk | CNAME → Certificate Manager's DNS authorization | grey | Lets Google issue and renew the origin certificate (section 05). A proxied answer would break the check |
Two connections, two TLS legs: the SSL/TLS mode
Builds on25. TLS and certificates
With an orange record there are two separate connections, each with its own TLS: the edge leg (shopper to Cloudflare) and the origin leg (Cloudflare to Kadé's load balancer). Cloudflare's SSL/TLS encryption mode controls only the origin leg. It is the most important single Cloudflare setting, and the names are easy to misread.
The SSL/TLS mode only changes the origin leg
Nothing is encrypted anywhere
No TLS on either leg. Only for sites that should not exist.
Show as config
Cloudflare → SSL/TLS → Overview → Encryption mode: Off Edge leg: HTTP Origin leg: HTTP
| # | Mode | Origin leg | Checks the origin's certificate? | Use |
|---|---|---|---|---|
| 1 | Off | HTTP, and the edge leg is HTTP too | - | Never |
| 2 | Flexible | HTTP (unencrypted) | - | Never for anything real: the padlock is a lie for the second half of the trip |
| 3 | Full | HTTPS | no any certificate, even self-signed or expired | A stepping stone only |
| 4 | Full (strict) | HTTPS | yes valid, unexpired, matching the hostname, from a trusted CA or Cloudflare's Origin CA | Kadé's choice |
| 5 | Strict (SSL-only origin pull) | Always HTTPS, as Full (strict), even for visitors who used HTTP | yes | When the origin should never receive plain HTTP |
Settings for the edge leg live elsewhere in Cloudflare and are worth turning on together:
| # | Edge setting | What it does | Kadé's value |
|---|---|---|---|
| 1 | Always Use HTTPS | Cloudflare itself redirects http:// to https:// | on |
| 2 | Minimum TLS version | Refuses older TLS from visitors | TLS 1.2, matching kade-tls on the origin (chapter 12) |
| 3 | TLS 1.3 | Allows the newest, fastest handshake | on |
| 4 | HSTS | Tells browsers to use HTTPS only, for a set time | on, after HTTPS has worked for a few weeks. Hard to undo: browsers remember it |
Certificates on each leg
Each leg needs its own certificate, issued for the same hostnames but by different parties:
| # | Leg | Certificate | Issued and renewed by | Covers |
|---|---|---|---|---|
| 1 | Edge | Universal SSL | Cloudflare, automatically, free | kade.lk and one level of subdomains (*.kade.lk). Deeper names like a.b.kade.lk need an Advanced certificate (paid) |
| 2 | Origin, option A | Google-managed, DNS authorization (chapter 12) | Google, automatically | Trusted by every browser, so it still works if a record is ever switched to grey |
| 3 | Origin, option B | Cloudflare Origin CA certificate | Cloudflare, free, valid up to 15 years; uploaded to GCP as a self-managed certificate | Trusted only by Cloudflare. Fine with Full (strict), but a grey record would show browsers an untrusted certificate |
_acme-challenge CNAME, and never breaks if a record changes. The load balancer authorization flavour of Google-managed certificates is the one that fails behind orange records, because the domain no longer points at the load balancer.Origin lock: making Cloudflare the only way in
Cloudflare protects only traffic that goes through Cloudflare. The load balancer's IP 34.120.88.10 is not secret: it is in old DNS records, in scanners' databases, and anyone can find it. So the origin must refuse anything that did not come through Kadé's own Cloudflare zone. That takes three layers, one for each way around:
| # | Way around | Layer that closes it | Where |
|---|---|---|---|
| 1 | Straight to kade-api-xxxx.run.app | Cloud Run ingress internal-and-cloud-load-balancing | Chapter 10 |
| 2 | Straight to 34.120.88.10 | Cloud Armor allows only Cloudflare's IP ranges; default rule denies | kade-edge-policy |
| 3 | Through another Cloudflare account, pointed at Kadé's IP | Cloud Armor also requires a secret header that only Kadé's zone adds (a Cloudflare Transform Rule) | kade-edge-policy |
Try each way in, and switch layers off to see which one stops it:
Allowed: the shopper reaches kade-api
The normal path. Kadé's Cloudflare zone adds the secret header, the request comes from a Cloudflare address, so Cloud Armor allows it, and Cloud Run accepts it from the load balancer.
The real client IP, behind two proxies
Builds on33. X-Forwarded-For
Google's front end now sees a Cloudflare edge address as "the client" (chapter 12 section 06, Cloudflare preview). Cloudflare passes the shopper's address in its own headers:
| # | Header | Contains | Kadé's use |
|---|---|---|---|
| 1 | CF-Connecting-IP | The visitor's IP, one value, set by Cloudflare | The app's client IP, and Cloud Armor's rate-limit key |
| 2 | X-Forwarded-For | Visitor, then Cloudflare edge, then load balancer (GCP appends two) | Logs only |
| 3 | CF-IPCountry | Visitor's country code | Country rules in Cloud Armor, instead of origin.region_code, which now sees Cloudflare's location |
| 4 | CF-Ray | Cloudflare's ID for the request | Match a request across Cloudflare's and GCP's logs |
Who blocks what: Cloudflare or Cloud Armor
Both are web application firewalls. Running both is normal, but each rule should live in one place, decided by what that place can see:
| # | Job | Cloudflare (edge) | Cloud Armor (origin) |
|---|---|---|---|
| 1 | Large DDoS attacks | yes absorbed before reaching GCP | Backstop |
| 2 | Bots, managed WAF rules, country rules | yes sees the real visitor | Only through Cloudflare's headers |
| 3 | Rate limiting by visitor | yes native | By CF-Connecting-IP header |
| 4 | "Only Cloudflare may reach the origin" | Cannot: it is the thing being checked | yes origin lock |
| 5 | Last line if Cloudflare is misconfigured or bypassed | - | yes preconfigured WAF rules stay on |
Caching: one cache or two
Kadé now has two CDNs in a row. Each request for a product image can be answered by Cloudflare's cache, by Cloud CDN's cache, or by the Cloud Storage bucket:
On a release: purge Cloud CDN first, then Cloudflare. The other way, Cloudflare refills from Cloud CDN's old copy.
The API (api.kade.lk) skips both: a Cloudflare Cache Rule bypasses cache, and the API sends Cache-Control: no-store.
| # | Rule | Why |
|---|---|---|
| 1 | Cloudflare is the cache shoppers hit; Cloud CDN stays on kade-static only | Cloudflare is closer to shoppers. Cloud CDN then shields the bucket from Cloudflare's own cache misses across many edge locations |
| 2 | Never cache the API | A Cloudflare Cache Rule bypasses cache for api.kade.lk, and the API sends Cache-Control: no-store. Cloudflare does not cache JSON by default, but saying it explicitly avoids surprises |
| 3 | Let the origin's Cache-Control decide lifetimes | One source of truth for both caches |
| 4 | On release, purge both, inner first | Purge Cloud CDN, then Cloudflare. In the other order, Cloudflare can refill from Cloud CDN's old copy |
| 5 | Use versioned file names (app.3f9c.js) | New names never need purging at all |
Other Cloudflare features worth knowing
| # | Feature | What it is | GCP equivalent or link |
|---|---|---|---|
| 1 | Transform Rules | Add, change or remove request and response headers at the edge | Used for the secret origin header (section 06) |
| 2 | Cache Rules, Configuration Rules, Redirect Rules | Per-path control of caching, security level and redirects | URL map redirects and Cloud CDN settings (chapter 12) |
| 3 | Cloudflare Tunnel | A small agent (cloudflared) on a VM opens an outgoing connection to Cloudflare, so the origin needs no public IP or load balancer at all | A different design: suits a VM-hosted app; it uses Cloud NAT for its outgoing connection (chapter 7) |
| 4 | Cloudflare Access (Zero Trust) | Sign-in in front of an internal web app | IAP for HTTPS (chapter 6.1) |
| 5 | Cloudflare Load Balancing | Health-checked failover between origins at the DNS or proxy level | Usually unnecessary with a global GCP load balancer |
| 6 | Workers | Code running at Cloudflare's edge | Out of scope here |
| 7 | Logs (Logpush) | Cloudflare's request logs sent to storage or a SIEM | Join with GCP logs by CF-Ray (chapter 16) |
| 8 | Development Mode | Bypasses Cloudflare's cache for three hours | Handy while testing; remember it turns itself off |
When it breaks: reading Cloudflare errors
When the origin leg fails, Cloudflare shows its own error page with a 52x code. Each code points at a specific link, so it tells you where to look before you open any log. Pick one:
526 · Invalid SSL certificate. Full (strict) mode found a certificate that is expired, self-signed, or for the wrong hostname.
- Where to look
- Certificate Manager: is the certificate ACTIVE, and does it cover this hostname (chapter 12, command 6)? Is _acme-challenge still DNS only?
Back to Kadé
Commands in this chapter
Commands 1 to 4 are read-only. Commands 5 to 7 change the project.
Is a name proxied? Look at the DNS answer
- What it shows: The addresses visitors get for each name.
- What to look for: Cloudflare addresses (for example in
104.16.0.0/13or172.64.0.0/13) mean orange.34.120.88.10means grey, and that name skips Cloudflare.
dig +short kade.lkdig +short www.kade.lkdig +short api.kade.lkDid a response come through Cloudflare?
- What it shows: The response headers.
- What to look for:
server: cloudflareand acf-rayheader.cf-cache-status: HITon images means Cloudflare's cache answered.
curl -sI https://www.kade.lk/images/rice.jpg | grep -iE "server|cf-ray|cf-cache-status|via"Test the origin lock from outside Cloudflare
- What it shows: What happens when you skip Cloudflare and call the load balancer IP directly, with the right hostname.
- What to look for:
403from Cloud Armor. A200means the lock is open.
curl -s -o /dev/null -w "%{http_code}\n" \ --resolve api.kade.lk:443:34.120.88.10 https://api.kade.lk/v1/healthCheck the DNSSEC chain after the move
- What it shows: Whether a validating resolver accepts the answer.
- What to look for: The
adflag in the header line means validated.SERVFAILfrom a validating resolver points at a stale DS record.
dig +dnssec kade.lk @1.1.1.1 | grep -E "flags|status"Allow only Cloudflare and Kadé's secret header (changes the project)
- What it shows: The origin lock in kade-edge-policy: two allow rules (Cloudflare's 15 ranges split across them), each also requiring the header, and a default deny.
- What to look for: Command 3 returns 403, while the site still works through Cloudflare. Use your own long random header value, and add the same value in a Cloudflare Transform Rule first.
gcloud compute security-policies rules create 500 --security-policy=kade-edge-policy \ --expression="(inIpRange(origin.ip,'173.245.48.0/20') || inIpRange(origin.ip,'103.21.244.0/22') || ...) \ && request.headers['x-kade-edge'] == 'REPLACE-WITH-LONG-RANDOM-VALUE'" \ --action=allowgcloud compute security-policies rules update 2147483647 \ --security-policy=kade-edge-policy --action=deny-403Key the login rate limit on the real visitor (changes the project)
- What it shows: The chapter 12 rate limit, now counted per
CF-Connecting-IPinstead of per source IP. - What to look for: In the logs, blocked requests carry different visitor IPs instead of a few Cloudflare addresses.
gcloud compute security-policies rules update 100 --security-policy=kade-edge-policy \ --enforce-on-key=HTTP-HEADER --enforce-on-key-name=CF-Connecting-IPPurge Cloud CDN before Cloudflare (changes the project)
- What it shows: Clears Cloud CDN's copy of changed files. Then purge the same paths in Cloudflare.
- What to look for: The command waits until the purge is done. Only then purge Cloudflare.
gcloud compute url-maps invalidate-cdn-cache kade-url-map --path="/assets/*"