archive2026

nginx behind a Cloudflare Tunnel: real client IPs and a maintenance page

Why the usual Cloudflare IP allow-list is the wrong real_ip setting behind a Tunnel, and a small nginx config that also serves a proper 503 maintenance page.

3 min read#ops#cloudflare#nginx

My web server is a small cloud VM with no inbound ports open. Cloudflare Tunnel connects out from the server, so visitors reach nginx through Cloudflare without the origin being exposed. I chose it over port forwarding because it hides the origin and also works behind CGNAT, and over a VPN because visitors cannot be asked to install a client. Admin access stays on a private path.

Most nginx snippets you find for Cloudflare assume a different setup. Copied into this one, they look fine and do nothing.

The setup

Browser -> Cloudflare edge -> Tunnel -> cloudflared -> nginx :8080 -> static files

cloudflared runs on the server and forwards each request to http://localhost:8080. In the Cloudflare dashboard the tunnel’s public hostname points at that service address. nginx never sees Cloudflare’s own IP addresses. It sees cloudflared.

Cloudflare’s IP ranges do not apply here

The widely copied snippet looks like this:

real_ip_header CF-Connecting-IP;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
# ...dozens more ranges

set_real_ip_from lists the peers that are allowed to tell nginx the visitor’s address. That list makes sense when Cloudflare’s servers connect to your origin directly (a normal proxied DNS record). Behind a Tunnel the TCP peer is cloudflared on localhost or a container network, so none of those ranges match. nginx then ignores the CF-Connecting-IP header and logs the internal address for every request.

The correct list is whatever cloudflared connects from:

set_real_ip_from 127.0.0.1;        # cloudflared on the same host
set_real_ip_from 172.16.0.0/12;    # or a Docker bridge network
real_ip_header   CF-Connecting-IP;

Because the origin accepts connections only from cloudflared, trusting that one header is safe. Do not use this on a server that is also reachable directly from the internet, since anyone could then forge the header.

Verify it:

tail -f /var/log/nginx/access.log
# the first field should be your own public IP, not 127.0.0.1 or 172.x

Proxy headers with nothing to proxy

Another snippet adds proxy_set_header CF-Connecting-IP ... lines. proxy_set_header only affects requests that nginx forwards with proxy_pass. In a static-file location it does nothing, and nginx passes incoming headers through by default anyway when you do proxy.

A maintenance page

While rebuilding the site I wanted every page to show one maintenance page, but with the right HTTP semantics:

server {
  listen 8080;
  server_name example.com;
  root /var/www/site;

  set_real_ip_from 127.0.0.1;
  set_real_ip_from 172.16.0.0/12;
  real_ip_header   CF-Connecting-IP;

  location /downloads/ { }          # keep files reachable

  location / { return 503; }

  error_page 503 @maintenance;
  location @maintenance {
    rewrite ^ /index.html break;
    add_header Retry-After 3600 always;
    add_header Cache-Control "no-store" always;
  }
}
  • return 503 is the whole switch. Anything written after it in the same location is unreachable, so keep that block to one line.
  • Retry-After tells crawlers the outage is temporary.
  • Cache-Control: no-store stops browsers and the edge from holding on to the maintenance page after you go live.

Going live means replacing location / { return 503; } with try_files $uri $uri/ =404; and reloading nginx.

One more thing: rsync –delete

If you deploy with rsync --delete, anything under the web root that is not in your build output is removed. A /downloads/ directory placed inside the web root will vanish on the next deploy. Keep it elsewhere and expose it with an alias, or exclude it from the sync.

Limits

The Tunnel is mostly for HTTP(S). Non-HTTP services like SSH or databases need extra client-side setup if the public should reach them. Large media or video delivery through the proxy is another case; check Cloudflare’s terms for your plan first. And the site now depends on Cloudflare. For a personal site I accept that.

Cloudflare WARP is a different product, even though the names come up together. It is a client that connects a device to Cloudflare’s network, for reaching private resources, not for publishing a site.