Web security / Cloudflare
How to secure a Cloudflare Pages website without breaking production
A secure website is not one setting: it combines account security, DNS hygiene, safe deployment practices, carefully chosen response headers and a clear recovery plan. This guide concentrates on non-intrusive checks you can perform on sites you administer.
What you need
- Cloudflare account access for a domain you own or are authorized to manage.
- A local copy of the complete site and a distinct preview branch.
- An inventory of external dependencies such as analytics, forms, AdSense and other scripts.
1. Secure the account first
Use multi-factor authentication, protect your recovery methods and review who can access the account. Team members should receive only the privileges they actually need. Review API tokens and revoke those no longer used.
2. Inventory DNS records
List DNS records and identify mail delivery, ownership verification, Pages and other services. Do not delete unfamiliar TXT, MX or CNAME records until you understand them. A change to mail DNS can interrupt email or sender verification.
3. Check HTTPS and redirects
Use a browser and a read-only curl check to inspect the actual responses for your apex domain and www subdomain. Confirm the intended canonical address, and investigate inconsistent content rather than assuming HTTP 200 is sufficient.
4. Use dedicated preview deployments
Test HTML, CSS, JavaScript, legal-page links and Functions in a non-production Pages branch. A successful upload does not prove all application routes work. Verify the actual page title and content, not just the status code.
5. Review HTTP security headers
Cloudflare Pages supports a _headers configuration file for static responses. Consider X-Content-Type-Options and Referrer-Policy as straightforward starting points. More restrictive policies such as Content-Security-Policy need testing against every required service, including external advertising and verification scripts.
6. Understand Pages Functions
Functions routes such as /api/... require function files and deployment from the correct project folder. Check that the API returns the expected JSON. Avoid putting private API keys in static HTML or JavaScript.
7. Make missing pages return real errors
A missing 404.html may cause Cloudflare Pages to fall back to SPA-style routing for unknown URLs. On a normal multi-page static site, a dedicated 404.html helps prevent broken links from silently returning the homepage. Test deployment routes and redirects before adding it.
8. Inspect robots.txt, sitemap.xml and ads.txt separately
A valid XML sitemap and a real plain-text ads.txt file are different from a website homepage accidentally returned for those URLs. Preserve legitimate verification and advertising records throughout deployments.
9. Maintain a rollback plan
Keep a known-good full release and a local backup. Record the last functioning Pages deployment. If a new release breaks important routes, use the deployment controls to roll back after checking your account permissions.
Commands to keep handy
Run these only in the context described above. Review each command and understand its effects.
curl -sIL https://YOUR-DOMAIN.example/ | grep -Ei "^HTTP/|^location:|^content-type:"
curl -fsSL https://YOUR-DOMAIN.example/robots.txt
curl -fsSL https://YOUR-DOMAIN.example/ads.txtFrequently asked questions
Does enabling every security header automatically improve security?
No. A misconfigured CSP can block legitimate scripts, while unrelated headers cannot fix compromised credentials or unsafe application logic.
Should I disable Cloudflare security when a bot receives 403?
Not globally. First inspect Cloudflare Security Events and the exact request, then make a narrow adjustment only if legitimate traffic is blocked.
Does a 200 response prove a page exists?
No. If Pages returns the homepage for an unknown URL, a broken link can still show HTTP 200.