Security

Proxying through Cloudflare is not enough: lock the origin too

In this article3 sections

Moving a site behind Cloudflare gives you filtering, caching and a hidden origin address. Many people stop there. But the protection only applies to visitors who actually come through Cloudflare. Anyone who finds your server’s real address can talk to it directly and skip every rule you set.

How the origin leaks

The real address often escapes in ordinary ways: an old DNS record from before the move, a mail server on the same address, a certificate transparency log, a forgotten subdomain, or a plain scan of the internet for web servers. Once an attacker has it, they can send a request straight to your server with your domain name in the Host header and bypass the firewall rules, the rate limits and the country rules you configured at Cloudflare.

The fix in three layers

1. Answer nothing for unknown names. Make a catch-all site that closes the connection, so a request by bare address or with a wrong name gets no page at all.

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;
}
server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    server_name _;
    ssl_reject_handshake on;
}

2. Accept only Cloudflare’s addresses. Cloudflare publishes its ranges at cloudflare.com/ips-v4 and cloudflare.com/ips-v6. Build a map from them and refuse everyone else on your real sites, but keep loopback allowed for your own tools.

geo $realip_remote_addr $cf_peer {
    default           0;
    127.0.0.1         1;
    ::1               1;
    173.245.48.0/20   1;
    # ...the remaining Cloudflare ranges
}

server {
    listen 443 ssl;
    server_name example.com;
    if ($cf_peer = 0) { return 444; }
    # ...
}

3. See the real visitor. Behind a proxy, every request arrives from a Cloudflare address, so your logs, rate limits and applications see the wrong client. The real-IP module fixes that:

set_real_ip_from 173.245.48.0/20;   # repeat for every range
real_ip_header CF-Connecting-IP;

Things that bite

  • Ranges change. Refresh the list on a schedule, for example weekly, and reload nginx only when the file really changed.
  • Certificate renewals. The common HTTP challenge also arrives through Cloudflare when the domain is proxied, so keep port 80 able to answer the challenge path.
  • Your own checks. Monitoring from the server itself uses loopback, which is why loopback stays allowed.
  • Test from outside. Send a request straight to the origin with the right Host header and confirm it is dropped: curl --resolve example.com:443:YOUR_ORIGIN_IP https://example.com/. A dropped connection is the correct result.
  • Fix the leak too. Rotate or remove old DNS records that point at the origin, and keep mail on its own address if you can.

This is one of the layers in our defense in depth setup. To see how your own server compares, run the health check or ask for an audit.