Cloudflare in front
Once the orange cloud is on, some behavior is no longer decided by your nginx. This chapter covers only the switches that change behavior, and the three limits you will inevitably encounter.
← Documentation index · Previous: Edge nginx · Back to: Deployment overview
1. Decide which domains go through the CDN
| Domain | Through the CDN? | Why |
|---|---|---|
| Site, console, identity, gateway (business domains) | Yes (orange cloud) | Attack protection, static asset caching, hides the origin IP |
Cluster API (the one kubectl uses) |
No (grey cloud) | The WebSocket of exec / logs and the mass of streaming responses are a poor fit for a proxy, and it should not be exposed to the public network |
| Interfaces that any client must reach directly (such as debugging payment callbacks) | It depends | Behind the CDN the origin sees a Cloudflare IP; signed callbacks are unaffected |
Grey cloud just points the DNS record at the origin and skips Cloudflare — certificates, ports and rate limiting are all your nginx's job.
2. DNS and SSL/TLS mode
- DNS: add an
A(orAAAA) record for each business domain pointing at the origin IP, and turn on the orange cloud. The proxy status is the switch. - SSL/TLS → Overview: choose Full (strict) as the mode.
Flexiblemakes Cloudflare go back to the origin over HTTP and the origin believes it is plaintext — redirect loops and lost Secure cookies both start here;Fulldoes not validate the certificate, which gives up the authenticity of the origin-pull segment; use it temporarily only when the origin has nothing but a self-signed certificate.
- Edge Certificates: minimum TLS version 1.2; turn on
Always Use HTTPS;Automatic HTTPS Rewritesis optional. - Origin pull certificate, pick one:
- The origin nginx already has a Let's Encrypt certificate (previous chapter) → do nothing;
- If you want Cloudflare to connect straight to the NodePort (skipping nginx) → use Cloudflare Origin CA to sign a 15-year certificate and install it on
Istio's
Gateway; the mode is still Full (strict).
Origin CA certificates are not trusted by browsers and can only be used for Cloudflare origin pulls. They do not replace the site certificate — the two directions each use their own.
3. Restore the real client IP
Skip this step and the origin logs are full of Cloudflare addresses, and rate limiting, risk control and troubleshooting by IP all stop working.
# Pull the official address ranges (they change; update them regularly)
curl -s https://www.cloudflare.com/ips-v4 https://www.cloudflare.com/ips-v6 \
| sed 's/^/set_real_ip_from /; s/$/;/' | sudo tee /etc/nginx/conf.d/cloudflare-realip.conf
In the site's server block (it must sit outside location, otherwise it only applies to one path):
real_ip_header CF-Connecting-IP;
Verify: tail -f /var/log/nginx/access.log; $remote_addr should become the real public address instead of 172.x.
Ubuntu's official nginx package is compiled with http_realip_module; if you built your own, confirm the module is included.
At the same time, restrict the origin firewall to the Cloudflare ranges (
ufw allow from <range> to any port 443), otherwise anyone who knows the origin IP can bypass the CDN and hit it directly.
4. Caching: static assets only
By default Cloudflare caches only its "cacheable extensions" (css/js/images), which is basically right. Do not use a
Cache Everything rule to override pages and APIs:
| Path | Rule | What happens if you don't |
|---|---|---|
/_next/static/*, images, fonts |
Cache (a long TTL is fine) | Goes back to the origin every time; static assets drag down first paint |
| Page HTML | Bypass | A logged-out user sees the previous user's content, or a logged-in user is cached as "logged out" |
/graphql |
Bypass | Sessions and permissions get cached, giving "switch accounts and still see old data" |
/api/auth/*, /events (SSE) |
Bypass | Login fails, and the event stream is buffered or cached |
| Uploads and webhooks | Bypass | The request body is truncated at the caching layer |
If the site uses a version suffix such as staticAssetVersion, static assets can safely be cached long; otherwise use a shorter TTL
(say 1 day) and rely on filename changes to update them.
5. Three limits you will definitely hit
| Limit | Cloudflare value | Symptom when you hit it | What to do |
|---|---|---|---|
| Single request body size | Free/Pro 100 MB | The upload reaches 100% and returns 413, and it has nothing to do with nginx's client_max_body_size |
Send uploads over 100 MB through a grey-cloud subdomain, or upgrade the plan |
| Origin response timeout | 100 seconds | A long task returns 524 while the log shows the origin still running | APIs behind the CDN must "return first, then run", or keep the connection alive with an SSE heartbeat |
| Single-connection idle | Decided at the edge | SSE with no data for a long time gets dropped | Send a comment-line heartbeat (: ping) from the server every 15–30 seconds |
The second one is the easiest to misjudge: 502/504 is the origin's problem, 524 always means Cloudflare gave up waiting. When you see 524, do not dig through Pod logs for an exception — there is nothing there, and the request is still running normally.
6. WebSocket
Cloudflare supports WebSocket by default, with no switch to turn on, but there are two things to note:
- You must use wss (the same port as
https://), otherwise the browser refuses it on an HTTPS page; - The
Upgrade/Connectionpass-through on the nginx side still has to be configured — Cloudflare only forwards the handshake to you, it does not upgrade for you. See themapblock in section 3 of Edge nginx.
7. How Certbot renews behind the CDN
When the certificate lives on the origin nginx, HTTP-01 validation requires http://titanides.com/.well-known/acme-challenge/xxx
to be reachable from the public network. Under the orange cloud this still holds (Cloudflare goes back to the origin), but two things break it:
Always Use HTTPS301s the validation request to HTTPS — Certbot follows it, so this usually does no harm; if you added a forced redirect to another domain, it will fail;- Restricting the origin to the Cloudflare ranges is correct, but 80 must be allowed as well (HTTP-01 uses 80).
The more reliable approach is DNS-01 (signed with a Cloudflare API token, supports wildcards):
sudo apt install python3-certbot-dns-cloudflare
sudo certbot certonly --dns-cloudflare \
--dns-cloudflare-credentials /etc/letsencrypt/cloudflare.ini \
-d 'titanides.com' -d '*.titanides.com'
Put an API Token in cloudflare.ini that has Zone:DNS:Edit on the target zone only (not a Global Key),
then sudo chmod 600. After switching, run certbot renew --dry-run once on the renewal timer.
8. Pre-launch checklist
# 1. Origin pull mode: the response headers should carry only one CF-Ray layer, and https works
curl -sI https://titanides.com/ | grep -Ei 'cf-ray|server|location'
# 2. The real IP is restored (run on the origin; you should see the real public address, not a Cloudflare range)
tail -n 5 /var/log/nginx/access.log
# 3. The cache does not hit dynamic paths (X-Cache should be MISS or BYPASS)
curl -sI https://titanides.com/graphql -X POST | grep -i 'cf-cache-status'
# 4. Where the large-file limit really sits (use a file slightly over 100 MB; a 413 means the CDN stopped it)
head -c 120000000 /dev/zero > /tmp/big.bin
curl -sS -o /dev/null -w '%{http_code}\n' -F 'file=@/tmp/big.bin' https://titanides.com/<upload-endpoint>
Item 4 matters because it tells you who stopped it: a 413 from nginx leaves a line in the origin access log; one from Cloudflare leaves nothing — the log is clean.