Cloudflare in front of the site: static cache, WAF, rate limiting and problems with Vercel
What Cloudflare does and doesn't do
Enabled proxying (orange cloud in DNS) puts Cloudflare between the visitor and your server. It terminates TLS, can serve cached responses, filter requests with WAF rules, and rate limit. But it only protects the traffic that goes through it. If the origin server IP is known, the attacker accesses it directly, and all rules are ignored.
Therefore, the setup goes in order: encryption to origin, closing origin, cache, rules.
1. SSL mode: Full (strict)
In SSL/TLS → Overview, select Full (strict). Cloudflare will connect to origin over HTTPS and verify the certificate.
- Flexible encrypts only the section from the browser to Cloudflare, and goes to the server via HTTP. The user sees a lock, and traffic to origin is open. Plus a classic loop: the server redirects HTTP to HTTPS, Cloudflare comes again via HTTP -
ERR_TOO_MANY_REDIRECTS. - For strict on origin, you need a valid certificate: Let's Encrypt or a free Origin Certificate from Cloudflare (it is trusted only by Cloudflare, browsers will not accept it - it is not suitable for direct visits).
2. Close origin
Options for increasing reliability:
- 1.Allow incoming 80/443 only from Cloudflare ranges (the list is published on cloudflare.com/ips) at the host or cloud firewall level.
- 2.Authenticated Origin Pulls: Cloudflare presents a client certificate, nginx only allows requests with it.
- 3.Cloudflare Tunnel (
cloudflared): the server itself establishes an outgoing connection, incoming ports are not open at all.
Don't forget that old DNS records (mail, ftp, direct) reveal IP without proxying. After moving to Cloudflare, changing your IP origin is a smart move.
The real address of the visitor comes in the CF-Connecting-IP header. For nginx:
# real_ip.conf — список диапазонов обновляйте из cloudflare.com/ips set_real_ip_from 173.245.48.0/20; set_real_ip_from 103.21.244.0/22; # ... остальные диапазоны IPv4 и IPv6 real_ip_header CF-Connecting-IP;
Without this, in the application logs and limits, all requests will be from Cloudflare addresses. You can trust this header only if origin is closed from direct requests - otherwise anyone will substitute it.
3. Cache
By default, Cloudflare caches static files by extension (images, CSS, JS, fonts) and does not cache HTML. It takes into account Cache-Control from origin. For Next.js, /_next/static/* already comes with public, max-age=31536000, immutable - these files are hash versioned and cached securely.
Cache HTML only consciously, through Cache Rules:
Правило: кэшировать публичные страницы блога
Когда: (http.host eq "example.com" and starts_with(http.request.uri.path, "/blog/")
and not http.cookie contains "__Host-session")
Действие: Eligible for cache, Edge TTL — использовать заголовки originThe main danger is personal pages in the general cache. The response with Set-Cookie or session-dependent will go to the next visitor. Rule: everything that depends on the user is served with Cache-Control: private, no-store, and the cache rule excludes requests with session cookies.
Check: response header cf-cache-status - HIT, MISS, DYNAMIC (not cached according to the rules) or BYPASS.
4. WAF Rules
Custom rules are written in the Cloudflare expression language. Examples that pay off on almost any site:
# Блокировать сканирование чужих CMS на сайте без WordPress
(http.request.uri.path contains "/wp-login.php" or http.request.uri.path contains "/xmlrpc.php"
or http.request.uri.path contains "/.env")
→ Block
# Админка только из офиса
(starts_with(http.request.uri.path, "/admin") and not ip.src in {198.51.100.0/24})
→ Block
# Managed Challenge для входа из стран, где у вас нет пользователей
(http.request.uri.path eq "/api/auth/login" and not ip.src.country in {"KZ" "RU" "UZ"})
→ Managed ChallengeFirst, set the action Log (on paid plans) or see Security → Events after enabling: a rule with an error in the expression can block real users or webhooks of payment systems. Exclude webhooks (Stripe, Telegram, Kaspi) from the challenge rules: the server will not pass JavaScript verification.
5. Rate limiting
Frequency limiting rules count requests by attribute (usually IP) on a path per period and take action if exceeded. To log in: no more than 10 POST requests to /api/auth/login per minute from one IP, then blocked for several minutes. Features depend on the plan: a limited number of rules and periods are available on the free plan, check the documentation for your plan.
An IP limit does not stop distributed password guessing. It complements, rather than replaces, the account-level failure counter.
Cloudflare before Vercel
Vercel does not recommend putting a third-party reverse proxy in front of your deployments, and in practice this creates problems:
- Two CDNs with independent caches. Invalidation on Vercel (ISR,
revalidateTag) does not reset Cloudflare cache and users see old pages. - Firewall and DDoS protection on Vercel see all requests from Cloudflare IP: IP limits and analytics become meaningless.
- Issuing certificates on Vercel may break if domain verification goes through a proxy.
If the domain is on Cloudflare and the site is on Vercel, leave the site records in DNS only mode (gray cloud). Cloudflare proxy makes sense for self-hosted servers and VPS, where there is no CDN or WAF.
Diagnostics
525 SSL handshake failed/526 Invalid SSL certificate- problem with the origin certificate in strict mode.521 Web server is down- origin does not accept connections from Cloudflare (often a firewall without Cloudflare ranges).- Users complain about captcha - look in Security → Events to see which rule worked.
When Cloudflare is redundant
Platforms with their own CDN and WAF (Vercel, Netlify) already provide the same. It is better to close internal services without public access with a VPN or Zero Trust access, rather than publish them behind a proxy.