NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Using Cloudflare with Your Own Server: SSL Modes, Real Visitor IPs and Origin Protection

Cloudflare sits between visitors and your server: it hides your server’s IP, absorbs many attacks, caches static content and provides free SSL at the edge. Set up carelessly, it also causes redirect loops, logs full of Cloudflare IPs instead of visitors, and a false sense of security when the origin is still reachable directly. This guide sets it up properly for a site on your own server.

How the proxy works

With the orange cloud (proxied) on a DNS record:

Visitor ──HTTPS──▶ Cloudflare edge ──HTTP/HTTPS──▶ your server (origin)

Visitors see Cloudflare’s IP addresses, not yours. Cloudflare connects to your server on their behalf. With the grey cloud (DNS only), the record simply points to your IP and Cloudflare is not involved in the traffic.

Step 1: Add the site and DNS records

  1. Add your domain in Cloudflare and change the nameservers at your registrar to the two Cloudflare gives you.
  2. Create A records for @ and www pointing to your server’s IP, proxied (orange).
  3. Keep records that must reach the server directly grey: mail (mail.example.com for MX), SSH hostnames, and anything not using HTTP/HTTPS on standard ports.

Cloudflare’s proxy only handles HTTP/HTTPS on specific ports. SSH, databases and mail cannot go through the orange cloud. A proxied record that is also used for SSH will not work — and a grey record on the same IP reveals your origin. Use a separate hostname or connect to SSH by IP.

Step 2: SSL mode — use Full (strict)

Mode Visitor → Cloudflare Cloudflare → origin Verdict
Off HTTP HTTP Do not use
Flexible HTTPS HTTP Avoid: traffic to your server is unencrypted, causes redirect loops
Full HTTPS HTTPS, any certificate Better, but certificate not verified
Full (strict) HTTPS HTTPS, valid certificate Correct choice

“Flexible” plus a server that redirects HTTP to HTTPS is the classic infinite redirect loop: Cloudflare requests http://, the server redirects to https://, Cloudflare requests http:// again.

For Full (strict), your server needs a certificate Cloudflare trusts. Two options:

Option A: Let’s Encrypt on the server

Works as usual — see Nginx and Let’s Encrypt. With the proxy on, the HTTP-01 challenge still works because Cloudflare forwards /.well-known/acme-challenge/ requests. Alternatively use the DNS-01 challenge with the Cloudflare DNS plugin.

Option B: Cloudflare Origin Certificate

In SSL/TLS → Origin Server, create a certificate (valid up to 15 years) for example.com and *.example.com. Save the certificate and private key on the server:

sudo mkdir -p /etc/ssl/cloudflare
sudo nano /etc/ssl/cloudflare/example.com.pem      # certificate
sudo nano /etc/ssl/cloudflare/example.com.key      # private key
sudo chmod 600 /etc/ssl/cloudflare/example.com.key
server {
    listen 443 ssl;
    http2 on;
    server_name example.com www.example.com;
    ssl_certificate     /etc/ssl/cloudflare/example.com.pem;
    ssl_certificate_key /etc/ssl/cloudflare/example.com.key;
    ...
}

Origin certificates are trusted only by Cloudflare. If you ever switch the record to grey, browsers will show an error. Let’s Encrypt avoids that dependency.

Then set the SSL mode to Full (strict) and enable Always Use HTTPS.

Step 3: Restore real visitor IPs

Behind the proxy, Nginx sees Cloudflare’s IPs. Logs become useless, and rate limiting or Fail2ban would ban Cloudflare itself. Cloudflare sends the visitor’s IP in the CF-Connecting-IP header; tell Nginx to trust it only from Cloudflare’s ranges.

Create /etc/nginx/conf.d/cloudflare-realip.conf:

# Current list: https://www.cloudflare.com/ips-v4 and /ips-v6
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
# add the IPv6 ranges as well
real_ip_header CF-Connecting-IP;

The ranges change occasionally. A small script run monthly from cron can download the lists and regenerate this file:

#!/usr/bin/env bash
set -euo pipefail
OUT=/etc/nginx/conf.d/cloudflare-realip.conf
{
  for v in 4 6; do
    curl -fsS "https://www.cloudflare.com/ips-v$v" | sed 's/^/set_real_ip_from /; s/$/;/'
  done
  echo "real_ip_header CF-Connecting-IP;"
} > "$OUT.new"
mv "$OUT.new" "$OUT"
nginx -t && systemctl reload nginx

For Apache, the equivalent is mod_remoteip with RemoteIPHeader CF-Connecting-IP and RemoteIPTrustedProxy lines.

Step 4: Lock the origin to Cloudflare

If attackers find your server’s real IP (old DNS records, a subdomain left grey, emails sent from the server, certificate transparency logs), they can bypass Cloudflare entirely. Allow ports 80/443 only from Cloudflare:

# UFW example
sudo ufw delete allow 'Nginx Full'
for ip in $(curl -fsS https://www.cloudflare.com/ips-v4) $(curl -fsS https://www.cloudflare.com/ips-v6); do
  sudo ufw allow proto tcp from "$ip" to any port 80,443
done

Stronger still: Authenticated Origin Pulls, where Cloudflare presents a client certificate that Nginx verifies (ssl_verify_client on; with Cloudflare’s CA). Requests without it are rejected even if they come from a Cloudflare IP used by another account.

If the origin IP was already exposed, changing to a new IP (for example a new additional IP on the server) after locking things down is worth considering.

Step 5: Caching

By default Cloudflare caches static files by extension (images, CSS, JS) but not HTML. Useful settings:

  • Browser Cache TTL: respect your server’s headers, which you control with Nginx caching rules.
  • Cache rules to cache HTML for anonymous visitors on content sites — but bypass for cookies such as wordpress_logged_in, wp-, woocommerce_ and for /wp-admin, cart and checkout.
  • Purge the cache after deployments (or use a plugin that purges on publish).

Cloudflare and visitors in mainland China

Cloudflare’s standard network does not have nodes inside mainland China (its China network requires an ICP filing and a separate enterprise arrangement). Chinese visitors are typically served from nodes in Hong Kong, Japan, Singapore or the US, depending on their carrier’s routing. Results vary: for some sites it is fine; for others, a Hong Kong server on a China-optimised route serving directly is faster and more consistent, especially at evening peak.

Test both ways from China (ITDOG, 17CE, or a machine in China) before deciding. See speeding up a website for China and China carrier routes.

Security features worth enabling

  • WAF managed rules (on paid plans) and custom rules to challenge /wp-login.php and /xmlrpc.php.
  • Bot Fight Mode for simple bot traffic (test that it does not block your own integrations).
  • Under Attack mode only during an attack — it shows a challenge page to every visitor.
  • Rate limiting rules in addition to Nginx rate limiting on the origin.

Cloudflare protects HTTP/HTTPS only. Attacks on other ports or directly on the server’s IP still need protection at the hosting network — see DDoS protected hosting.

Troubleshooting

Error Meaning Fix
Too many redirects Flexible SSL + HTTPS redirect on origin Use Full (strict)
521 Web server is down Cloudflare cannot connect to origin Nginx running? Firewall allows Cloudflare IPs?
522 Connection timed out Origin too slow or packets dropped Firewall, server load, routing
525 / 526 SSL handshake / invalid certificate Origin certificate problem Check certificate, hostname, Full (strict)
Logs show only Cloudflare IPs Real IP not restored Step 3