Why your server sees the wrong IP
Without a proxy, a visitor's browser connects straight to your server, and the address of that connection — REMOTE_ADDR in PHP, $remote_addr in nginx — is the visitor's. Put a CDN, a load balancer or any reverse proxy in front, and the connection your server accepts comes from the proxy. Every log line, every rate limit, every "sign-in from a new location" check now sees the proxy's address.
The visitor's address is not lost. The proxy saw it and writes it into the request it forwards, in a header. The whole topic comes down to two questions: which header to read, and when to believe it — because a header is just text, and anyone who can reach your server directly can write any text they like.
How X-Forwarded-For is built
X-Forwarded-For (XFF) is a comma-separated list. Every proxy that handles the request takes the existing value and appends the address it received the request from. A visitor at 198.51.100.7 going through one proxy arrives at your server as:
X-Forwarded-For: 198.51.100.7
Through two proxies, the second one appends the first proxy's address:
X-Forwarded-For: 198.51.100.7, 203.0.113.20
Here is the part that matters for security: a proxy appends; it does not check what was already there. A visitor can send their own X-Forwarded-For: 1.2.3.4, and once the CDN appends the real connecting address, your server sees:
X-Forwarded-For: 1.2.3.4, 198.51.100.7
The left-most value is whatever the visitor typed. The value you can trust is the one added by the proxy you trust — the right-most one, or, with several trusted proxies in a row, the first value from the right that is not one of your own proxies. Code that takes the first value of XFF as "the client IP" can be fooled by anyone with curl.
X-Real-IP and the cdn.com.tr headers
X-Forwarded-For carries a history; often you just want one answer. Single-value headers exist for that.
X-Real-IP is set by many nginx-based proxies to the address of the connection they accepted. Our edge sets it on every request.
X-CDN-Client-IP is the visitor's address as our edge saw it: a single value, IPv4 or IPv6, written by the edge on every request. A header of the same name sent by the visitor is replaced, not passed on. This is the simplest header to read.
X-CDN-Client-Port is the visitor's source port. Where many users share one public address — mobile networks, carrier-grade NAT — the address alone does not identify a visitor; address plus port does, and that is what you need when a log line must point to a single connection.
X-CDN-Edge-IP is the address of the edge server the visitor connected to, useful when you match your logs with ours or record where a connection was made.
These headers are standard on every request; there is nothing to switch on. The edge also sends X-Forwarded-For with the connecting address appended, and X-Forwarded-Proto, X-Forwarded-Host and X-Forwarded-Port for the scheme, hostname and port the visitor used.
Trust the header only from your CDN
A header the edge writes is trustworthy only when the request really came through the edge. If your origin answers anyone on the internet, an attacker can connect to it directly and send X-CDN-Client-IP: 127.0.0.1 or any address they like. Two measures close that gap, and they work best together.
Tell your web server which addresses are proxies. nginx, Apache and most frameworks restore the client address only for requests that come from addresses you list as trusted; from anyone else the headers are ignored. List the edge addresses for your account — we give you the current addresses on request.
Lock the origin to the CDN. Require a secret header whose name and value you choose and add to every delivery rule, or require the edge's client certificate with mTLS. Then a direct request never reaches your application at all, and the forwarded headers on the requests that do arrive are the edge's.
Make the secret header or the certificate the main lock and the address list the second one: they keep working however the edge addresses change, while a list has to be kept up to date.
nginx: set_real_ip_from and real_ip_header
nginx's realip module replaces $remote_addr with the address from a header — but only when the connection comes from an address listed in set_real_ip_from. After that, your access log, allow and deny rules and limit_req all see the visitor.
With X-CDN-Client-IP there is no list to parse. If you prefer X-Forwarded-For — for example because a load balancer of your own sits between the CDN and nginx — use real_ip_header X-Forwarded-For; with real_ip_recursive on;, and list both the edge addresses and your load balancer in set_real_ip_from. nginx then walks the list from the right, skips every trusted address and stops at the first one that is not: the visitor.
Keep the original connecting address in the log too ($realip_remote_addr). When you need to know which edge delivered a request, it is right there.
nginx: restore the visitor address from the CDN
# in http {} or server {}
# the edge addresses for your account (placeholders)
set_real_ip_from 203.0.113.0/24;
set_real_ip_from 2001:db8:100::/48;
real_ip_header X-CDN-Client-IP;
log_format cdn '$remote_addr:$http_x_cdn_client_port via $realip_remote_addr '
'[$time_local] "$request" $status $body_bytes_sent';
access_log /var/log/nginx/access.log cdn;
Apache: mod_remoteip
Apache's mod_remoteip does the same job. Enable it (a2enmod remoteip on Debian and Ubuntu), name the header and list the trusted proxies. Use RemoteIPTrustedProxy for public addresses; RemoteIPInternalProxy is meant for private ranges.
Use %a in the log format: with mod_remoteip configured it is the restored visitor address. Access rules such as Require ip see the visitor's address as well once the module is set up.
Apache 2.4 with mod_remoteip
RemoteIPHeader X-CDN-Client-IP
# the edge addresses for your account (placeholders)
RemoteIPTrustedProxy 203.0.113.0/24
RemoteIPTrustedProxy 2001:db8:100::/48
LogFormat "%a %{X-CDN-Client-Port}i %l %u %t \"%r\" %>s %b" cdn
CustomLog ${APACHE_LOG_DIR}/access.log cdn
Laravel and other frameworks: trusted proxies
Frameworks read X-Forwarded-For themselves, with the same rule: forwarded headers count only from trusted proxies. In Laravel 11 and later you declare them in bootstrap/app.php; $request->ip() then returns the visitor, and $request->secure() and generated URLs follow X-Forwarded-Proto.
Laravel, through Symfony, reads X-Forwarded-For from the right and skips trusted addresses — exactly the right-most-trusted rule. Do not trust * on an origin that the internet can reach directly; that tells the framework to believe any client's headers. Express (app.set('trust proxy', ...)) and Rails (config.action_dispatch.trusted_proxies) follow the same idea under other names.
bootstrap/app.php (Laravel 11+)
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(
at: ['203.0.113.0/24', '2001:db8:100::/48'], // edge addresses (placeholders)
headers: Request::HEADER_X_FORWARDED_FOR
| Request::HEADER_X_FORWARDED_HOST
| Request::HEADER_X_FORWARDED_PORT
| Request::HEADER_X_FORWARDED_PROTO,
);
})
// anywhere in the application
$ip = $request->ip(); // the visitor
$port = $request->header('X-CDN-Client-Port'); // the visitor's source port
Check that it works
Three quick tests confirm the setup.
Through the CDN. Request a page and look at your access log: the address must be your own public address, not an edge address.
Spoof it. Send curl -H 'X-Forwarded-For: 1.2.3.4' -H 'X-CDN-Client-IP: 1.2.3.4' https://www.example.com/. The log must still show your real address: the edge replaces X-CDN-Client-IP and appends your address after the XFF value you sent.
Go around the CDN. Send the same request straight to the origin with --resolve. If the origin is locked, the request is refused; if it is not, the log must show your real address rather than 1.2.3.4, because your address is not in the trusted list. If it shows 1.2.3.4, the trusted list is too wide.
Where the real IP matters
Access logs, analytics and abuse reports count your visitors, not the edge servers in front of them.
Per-IP limits and fail2ban-style blocks hit the abuser instead of everyone who reaches you through the same edge.
Address, source port, time and edge address together point to a single connection, even behind shared mobile addresses.
How to set it up
Step-by-step guides in the help centre:
X-Forwarded-For FAQ
Which value of X-Forwarded-For is the client IP?
The right-most value added by a proxy you trust. Values to its left were already in the header when the request reached that proxy, so the visitor may have written them. With one CDN in front of your server, that is simply the last value.
What happens to an X-Forwarded-For header the visitor sends?
The edge keeps it and appends the address it received the request from, as proxies do. That is why you read the right-most value — or X-CDN-Client-IP, a single value the edge writes itself and a visitor cannot set.
What is the difference between X-Forwarded-For and X-Real-IP?
X-Forwarded-For is a list that every proxy appends to, so it records the whole path. X-Real-IP is a single address set by one proxy. Both are conventions; the standard Forwarded header (RFC 7239) carries the same information with a stricter syntax.
Why would I need X-CDN-Client-Port?
Because many visitors can share one public IP address on mobile networks and behind carrier-grade NAT. The source port, together with the address and the time, tells those visitors apart, which is what you need when a log line must point to one connection.
Can I trust X-CDN-Client-IP from any address?
Only when your origin accepts no traffic except from the CDN, locked with a secret header or mTLS. If the origin can be reached directly, anyone can send that header, so restrict it to the edge addresses as well.
Where do I get the edge addresses to trust?
We tell you the current addresses of the edge servers linked to your account on request. Lock the origin with a secret header or mTLS as well, so your protection does not depend on keeping a list up to date.