Behind a Cloudflare Tunnel, Every Visitor Is 127.0.0.1
Through cloudflared, nginx sees a local process instead of a visitor. The real address arrives in a header, but nothing writes it down.

On this page
I opened access.log to settle one question: were requests to one of the sites really coming from outside, or was something on the box repeating them. Read the last few lines, look for a pattern.
There was no pattern. One address, on every line: 127.0.0.1.
Not most of them. All of them. I mentioned it in passing in an earlier article; this time I read the file to the end, and my reaction hadn’t changed.
First instinct: distrust the file. Maybe the log was written wrong, or there were two files and I had the wrong one open.
Only two log files are active: /var/log/nginx/access.log and /var/log/nginx/error.log, each declared once at the http level in /etc/nginx/nginx.conf. Neither vhost declares its own. link.demode.id.access.log, in the same directory, is zero bytes and referenced by nothing — leftover from an old vhost, not a log being written.
The format is nginx’s stock combined, no custom log_format anywhere, and its address column is just $remote_addr. No $http_cf_connecting_ip, no $http_x_forwarded_for. Even when the header carrying the real address reaches nginx, there is no column to put it in.
Which leads somewhere less comfortable. The log isn’t damaged, it’s honest. It reports exactly what nginx sees, and what nginx sees is a local process on the same machine. Not one line is wrong. My assumption about what that first column measures was.
A truthful log that answers the wrong question
The first column of access.log is the source address of the connection. Not a person, a device, or a network — just whoever opened the TCP socket to nginx. When that’s another process on the same box, the answer is 127.0.0.1, loopback, the address that means “myself”.
For years I read that file as a list of visitors. On a site whose port is open straight to the internet, the assumption happens to hold. Behind a tunnel it’s wrong, and stays invisible: the file is tidy, the line count grows daily, and none of it answers the question I came with.
One thing is missing, and it’s the whole point of the file: where the request came from.
Why this happens: a tunnel is an outbound connection
Cloudflare Tunnel reverses the direction. Nothing is opened for the internet to come in. What runs on the server is cloudflared, a process that dials out to Cloudflare’s network and keeps that connection alive. Visitor traffic rides inside it, enters through the end that’s already connected, and gets handed to nginx over a local address.
No TCP socket from the internet ever touches nginx. What touches it is cloudflared itself, a process on the same machine, reaching nginx over loopback. To the kernel, a conversation between two processes. To nginx, the source is its own neighbour. Not a guess, either — on this server cloudflared does run on the same box, with ingress pointed at http://localhost:80.
None of that is a flaw. It’s why the tunnel gets used: reachable without opening a port to the public, without certificates you renew yourself, without exposing a home address. The price: the source of the connection disappears at the network layer.
Nor is nginx being sloppy: the visitor’s address never arrives at that layer, because all that’s there is a local connection. To keep where a visitor came from, carry it at the application layer, as a header inside the HTTP request.

The same path, twice: without real_ip nginx only sees the local process; once the header is read, the visitor’s address is what gets written.
Everything else that trusts the address
Once the source address is wrong, the damage doesn’t stop at the log. Anything reading that address judges the wrong object, because all of it reads the same variable.
Logs and analytics. Visits from different cities, networks, and devices collapse into one identity. You can’t count visitors or trace an attack pattern, and a question as ordinary as “where are these people opening the page from” has no answer. What’s left is a list of timestamps and paths.
Rate limiting. This server has no limit_req, no limit_conn, no geo or map that could serve as a key. Nothing is broken yet. But add such a rule keyed on $remote_addr and every visitor shares one bucket: the busy one spends everyone’s allowance, the rest get 503s, and the cause is impossible to find in a log that records one address for everyone.
Firewalls and blocking. ufw is active here, but its rules are port-based; the only one naming an address limits port 5900 to the local network. nftables isn’t active, fail2ban isn’t installed, nginx has no address-based allow or deny — its single deny all covers dotfiles, which is path-based. So today nothing judges wrongly. Add a rule that reads the source address, though, and it will judge the wrong object: blocking 127.0.0.1 cuts nginx off from cloudflared. The site dies, not the attacker. Behind a tunnel the server firewall isn’t the front door anymore; that door is guarded on Cloudflare’s side.
Access by address. An admin page protected by a list of allowed addresses will either reject everyone or admit everyone, depending on how the list was written, because what gets compared against it is 127.0.0.1.
Different symptoms, one illness: something reads an address, then decides based on it.
The fix, once the model is right: headers and real_ip
With the model straight, the repair makes sense without memorising configuration.
Mine now: the thing that sees a connection is a local process, so the real address has to travel in a header — worth nothing unless something reads it.
That splits in two. First, the origin has to be sent along, and it already is. I captured loopback traffic with tcpdump while sending a single request through the tunnel. The TCP connection came from 127.0.0.1, but inside the request was already Cf-Connecting-Ip with the visitor’s address, plus X-Forwarded-For, Cf-Ipcountry, Cf-Ray, Cf-Visitor, and X-Forwarded-Proto. The address does reach nginx. What never happens is the recording: the log line still says 127.0.0.1.
Second, nginx has to be taught to read it. Under /etc/nginx there is not a single real_ip, set_real_ip_from, or real_ip_header directive. The closest thing is /etc/nginx/proxy_params, which sends X-Forwarded-For to the backend — passing a header along, not the realip module, and it doesn’t touch $remote_addr. The address comes in, goes through, and vanishes from the log.
The concept fits in two lines. Tell nginx which header to trust (real_ip_header), and from which peer that header may be trusted (set_real_ip_from). The second is where people slip, because the directive matches the TCP peer address, not the address written inside the header. Since cloudflared runs on the same machine and connects to localhost:80, the right value is 127.0.0.1, not Cloudflare’s IP ranges. Put the edge ranges there and it never matches, and it fails quietly: no error, just a log that still says 127.0.0.1. ::1 covers IPv6. Move cloudflared into a container later and the peer address changes — maybe to the docker gateway — so the value moves with it.
The effect runs through everything. Once $remote_addr holds the real address, everything reading it is right too: access.log, the limit_req key, allow rules, any variable in the configuration. No fixing them one at a time, because the problem was one value they share.
I’ve left the configuration order out on purpose. The values depend on topology: where cloudflared runs, which address nginx listens on, whether another proxy sits in between.
A header is not the truth
Easy to skip: anyone can write a header. If nginx trusts headers from every party, anyone who can send a request directly can claim any address they like — and the rate limiter you just repaired becomes a way to flood someone else’s address.
So trust has to be narrow: accept header values only from the address that is genuinely the way in. Here that path is one hop wide, cloudflared on the same machine, forwarding to localhost:80. That’s why 127.0.0.1 is a reasonable boundary, and why the consequence deserves saying. Anything else on this machine that can reach nginx is trusted too.
Chains bring their own trap. With more than one proxy layer, a header can hold several addresses in order, and the right one is the rightmost under its owner’s rules, not the leftmost, which a visitor can fill in. No error comes of it. You just get logs that look right and aren’t.
What’s left
The file is still where it was, and every line I opened that day still says 127.0.0.1. I haven’t fixed it. I’ve only just finished understanding why. For now that’s enough: while the model is wrong, any configuration feels correct and I have no way to prove it.
What changed is small. I no longer read access.log as a list of visitors, but as a record of who opened a socket to nginx — behind a tunnel, always the neighbour. To learn who the visitors are, I have to ask somewhere else: the header carrying the answer, and the configuration that decides whether to trust it.