Security

Cloudflare for a business website: what to turn on and what to avoid

In this article10 sections

Cloudflare sits between your visitors and your server. It answers DNS queries, forwards traffic, stores copies of static files close to visitors, encrypts the connection, filters malicious requests and absorbs floods. For a small business, a free account can make a site faster, harder to attack and cheaper to run. It can also break your email, your checkout, your admin login and your payment callbacks if it is set up by clicking through the defaults.

This guide covers the setup in the order that avoids the usual damage: DNS first (including the records that must never be proxied), then encryption, caching, security rules, real visitor addresses, and the one step most people skip: making sure attackers cannot simply go around Cloudflare to your server. Feature availability varies by plan and changes over time, so check Cloudflare’s documentation for what your plan includes.

What Cloudflare does, and what it does not

When a hostname is proxied (the orange cloud), visitors connect to Cloudflare, which connects to your server. Your server’s real address is hidden, traffic is filtered, and static files can be served from Cloudflare’s network. When a hostname is DNS only (the grey cloud), Cloudflare just answers the DNS query, and visitors connect to your server directly.

It does not make a slow application fast by itself, it does not fix a hacked site, and it does not replace updates, backups or a firewall on the server.

Step 1: Add the site and check every DNS record

Cloudflare scans your existing DNS and imports what it finds. Do not trust the import. Compare it with the current zone, record by record, before you change nameservers. Missing records are the usual cause of “email stopped working after the move”.

Then decide for each record whether it is proxied:

  • Proxy (orange): the website itself (example.com, www), and application hostnames served over HTTP or HTTPS.
  • DNS only (grey): mail. MX records are never proxied, and the hostnames they point to (mail.example.com) must be DNS only, because Cloudflare’s proxy does not carry SMTP or IMAP. If a mail hostname is proxied, mail breaks.
  • DNS only: anything that is not web traffic: SSH, FTP, a database, a game or VPN service on its own port.
  • TXT records (SPF, DKIM, DMARC, verification) are never proxied; they are just copied.

Lower the TTL on the old records a day beforehand if you can. Then change the nameservers at your registrar to the two that Cloudflare gives you, and keep the old DNS service running until Cloudflare reports the domain active and you have tested.

Step 2: Set the encryption mode correctly

This is the most common source of “too many redirects” errors. Under SSL/TLS, set the mode to Full (strict). In that mode Cloudflare connects to your server over HTTPS and checks that the certificate is valid. It needs a proper certificate on your server: a free Let’s Encrypt certificate works, and so does a Cloudflare Origin Certificate (which only Cloudflare trusts, so use it only for proxied hostnames). The two weaker options are traps:

  • Flexible sends traffic from Cloudflare to your server in plain HTTP. If your server then redirects HTTP to HTTPS, Cloudflare and your server redirect each other forever.
  • Full encrypts but does not check the certificate.

Also turn on Always Use HTTPS. Enable HSTS only after everything works over HTTPS, and start with a short lifetime. The diagnosis steps for certificate errors, including Cloudflare’s 525 and 526 errors, are in SSL certificate problems: how to diagnose and fix.

Step 3: Caching without breaking logins

By default Cloudflare caches static files (images, stylesheets, scripts) and does not cache HTML pages. That is a safe default. Caching HTML as well can make a WordPress site dramatically faster and can also show one visitor another visitor’s cart or admin bar if it is set up carelessly.

If you cache HTML, do it with rules that bypass the cache for everything personal:

  • Bypass for /wp-admin, /wp-login.php, the cart, checkout and account pages, and any request that carries a login cookie such as wordpress_logged_in.
  • Cache only pages that are the same for everyone.
  • Purge when you publish. A purge plugin or an API call from your site does this automatically.

Always test with the response headers:

curl -sI https://example.com/ | grep -iE 'cf-cache-status|cache-control|age'

cf-cache-status: HIT means it came from Cloudflare’s cache; MISS or DYNAMIC means it was fetched from your server. Check a logged-in session too, and make sure personal pages are never HIT.

Step 4: Security rules that help, and ones that hurt

Cloudflare offers managed rules for known attacks, custom rules you write, rate limiting, and bot protection. The useful ones for a small business:

  • Turn on the managed ruleset that your plan provides, and review what it blocks in the security events log.
  • Restrict your admin areas. If your team works from one country or a few addresses, a custom rule can block /wp-admin, /wp-login.php or a billing admin path for everyone else. Remember to switch it off when you travel.
  • Rate-limit login and search pages, so that password guessing and abusive scraping are slowed down.
  • Challenge, do not block, when unsure. A block rule with a mistake locks out customers.
  • Use “Under Attack” mode only temporarily, during an actual flood.

Two rules that hurt if you are careless:

  • Bot protection can challenge machines you want. Payment gateways, webhooks, uptime monitors, mobile apps and API clients are not browsers and cannot solve a challenge. Create explicit exceptions for your payment callback paths, and watch the security events log after any change. A blocked payment webhook means customers pay and your system never records it.
  • Country blocks cut off legitimate customers and travelling staff. Prefer challenges, and be precise.

Step 5: Give your server the real visitor address

Once traffic goes through Cloudflare, your server sees Cloudflare’s addresses, not your visitors’. Logs, rate limiters, fail2ban and plugins then treat all visitors as a handful of addresses, so one abusive visitor can get a whole region locked out. Cloudflare sends the real address in the CF-Connecting-IP header, and your server must be told to trust it, only when the request comes from Cloudflare’s published ranges.

For NGINX (the ngx_http_realip_module):

real_ip_header CF-Connecting-IP;
# one line per Cloudflare range, from https://www.cloudflare.com/ips-v4 and /ips-v6
set_real_ip_from 173.245.48.0/20;

Do not copy the example range: fetch the current lists from Cloudflare’s published pages and refresh them occasionally (a small weekly job does it). For Apache, use mod_remoteip with RemoteIPHeader CF-Connecting-IP and the same ranges. WordPress behind Cloudflare needs the same treatment for security plugins that block by address. Check afterwards that your access log shows real visitor addresses.

Step 6: Stop attackers going around Cloudflare

Hiding behind Cloudflare protects only traffic that goes through it. If your server accepts connections from anywhere, anyone who learns its real address (from old DNS records, an email header, a certificate search, or a scan of the address range) can attack it directly and bypass every rule you set.

Fix that at the server: accept web traffic only from Cloudflare’s ranges (and your own management address). The full method, including the nginx rule and keeping the list current, is in Proxying through Cloudflare is not enough: lock the origin too. Also change your server’s address if it has been exposed for a long time, and remove old DNS records that point at it.

Mistakes that cost businesses sales

  • Proxying the mail hostname, so that email stops.
  • Flexible SSL, so that the site loops or shows warnings.
  • Caching logged-in or cart pages, so that customers see each other’s data.
  • Challenging payment gateways and webhooks, so that orders are not recorded.
  • Turning on every optimisation switch (minify, Rocket Loader, early hints) at once, so that scripts break and nobody knows which switch did it. Enable one, test, then the next.
  • Leaving the origin open, so that the protection is optional.
  • Forgetting the dashboard login: protect the Cloudflare account itself with two-factor authentication, because whoever controls it controls your DNS.

A short rollout order

  1. Compare the imported DNS records; mark mail and non-web records as DNS only.
  2. Switch the nameservers; wait for active status; test.
  3. Set Full (strict) with a valid origin certificate; Always Use HTTPS.
  4. Leave caching at the defaults first; add HTML caching later with bypass rules.
  5. Add the managed ruleset, an admin restriction, and a login rate limit.
  6. Restore real visitor addresses on the server.
  7. Lock the origin.
  8. Turn on two-factor sign-in on the Cloudflare account.

When it is worth getting help

Cloudflare is easy to switch on and easy to misconfigure in ways that only show later: a month of lost email, a checkout that quietly fails for some customers. If you want a clean setup that covers DNS, encryption, caching rules, security rules, real addresses and the locked origin, I do this regularly, and I include a written record of every setting.

You can see my profile on Upwork, or read about server hardening and the email and DNS service.