Enabling Cloudflare proxying hides your origin server's IP address in DNS lookups, yet historical DNS archives, SSL certificate transparency logs, and internet-wide port 443 scanners still uncover VPS IPs within days. Once an attacker knows your origin IPv4 address, sending curl -H "Host: example.com" https://<origin-ip> bypasses Cloudflare WAF rules and edge DDoS scrubbing completely.
1. Layer 1: Authenticated Origin Pulls (mTLS) in Nginx
Cloudflare can present a client TLS certificate on every connection from its edge to your Nginx origin (Strict SSL mode + Authenticated Origin Pulls). Rejecting requests without that verified client certificate guarantees only Cloudflare edge nodes pass TLS handshakes:
# Inside Nginx server block listening on 443 ssl
ssl_client_certificate /etc/nginx/certs/cloudflare-origin-pull-ca.pem;
ssl_verify_client on;
Remember to also enforce Full (Strict) SSL mode in Cloudflare using a valid Let's Encrypt or Cloudflare Origin CA certificate on the VPS so traffic between edge and origin stays encrypted and authenticated in both directions.
2. Layer 2: Firewall Ports 80/443 Exclusively to Cloudflare CIDRs
Pairing mTLS with firewall rules drops unwanted origin TCP SYN handshakes before Nginx or TLS libraries even burn CPU:
# Refresh Cloudflare IPv4/IPv6 ranges cleanly into UFW (test carefully before removing fallback)
for ip in $(curl -fsSL https://www.cloudflare.com/ips-v4); do
sudo ufw allow proto tcp from "$ip" to any port 80,443 comment "Cloudflare Edge"
done
Always keep administrative SSH isolated exclusively on a private Tailscale/WireGuard interface (tailscale0) so you retain out-of-band server access regardless of CDN firewall updates.
3. Preserving Real Visitor IP Addresses Behind Cloudflare
Once all HTTP/HTTPS traffic arrives exclusively from Cloudflare edge ranges, every request socket inside Nginx reports a Cloudflare IP unless ngx_http_realip_module restores the original visitor address. Without that module, Nginx rate limits and PHP IP checks throttle Cloudflare's edge instead of individual clients:
# /etc/nginx/conf.d/cloudflare-realip.conf
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 172.64.0.0/13;
# Include full IPv4 & IPv6 list from cloudflare.com/ips via weekly timer
Keep automated Cloudflare CIDR refreshes inside a lightweight weekly systemd timer that validates nginx -t before reloading, and never bypass mTLS checks by testing HTTP directly over public IPs.