Put a site behind a CDN and your access log changes overnight. Every line comes from a handful of addresses you've never seen before:
www.example.com:443 172.70.34.112 - - [17/Nov/2026:09:14:02 +0000] "GET /wp-login.php HTTP/2.0" 404 196 "-" "Mozilla/5.0"
www.example.com:443 172.70.34.112 - - [17/Nov/2026:09:14:02 +0000] "GET /pricing HTTP/2.0" 200 18211 "https://www.google.com/" "Mozilla/5.0"
www.example.com:443 162.158.90.41 - - [17/Nov/2026:09:14:03 +0000] "GET /.env HTTP/2.0" 404 196 "-" "curl/8.4.0"
Those are Cloudflare edge servers. The visitor connected to the CDN, the CDN opened its own connection to you, and Apache or nginx logged the address on the other end of that TCP connection. The real visitor's IP is still there, in a request header. Your web server just isn't reading it.
The fix is a few lines of config: tell Apache or nginx which header holds the visitor's address and which proxies are allowed to set it. Below is how, for Cloudflare, Fastly and the NOC.org WAF/CDN.
With edge IPs in the log, everything that keys on the source address is looking at the CDN:
Trunc doesn't know your site is behind a CDN. The blocklist skips private, reserved and CGNAT ranges and nothing else, so a CDN address in your log is treated like any other address.
So fix the logs first, then turn Active Response on for web logs.
| CDN | Header with the visitor IP | Edge ranges |
|---|---|---|
| Cloudflare | CF-Connecting-IP (True-Client-IP on Enterprise, if you enable it) |
https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6 |
| Fastly | Fastly-Client-IP (only after the VCL change below) |
https://api.fastly.com/public-ip-list (JSON) |
| NOC.org | X-Real-IP (also in X-Forwarded-For) |
https://noc.org/ipv4 (plain text, one IPv4 address per line) |
Checked against each vendor's docs on 2026-11-17. Links at the end.
Cloudflare's CF-Connecting-IP holds a single address. True-Client-IP holds the same value, but it's Enterprise-only and you have to switch on a Managed Transform to get it, so there's no reason to prefer it.
Fastly adds Fastly-Client-IP to every request, but its own docs say that if the client sends that header, Fastly keeps the client's value. Out of the box, anyone can pick what lands in your log. The Fastly section below has a short VCL snippet that fixes that.
NOC.org sends X-Real-IP with a single address, the visitor's. It's in X-Forwarded-For too, but X-Real-IP is the one to use.
A request header is just text the client can type. Try it from any shell:
curl -H 'X-Forwarded-For: 198.51.100.7' https://www.example.com/
If your server takes X-Forwarded-For from anyone, that request gets logged as 198.51.100.7. An attacker picks their own logged IP. With Active Response on, they can pick yours, or your office's, or your payment provider's, and trip the scan threshold until it lands on your blocklist.
So both Apache and nginx need two things:
A request from an address that isn't on the list is logged with its real TCP peer address, header or no header. Then lock the origin so only the CDN can reach it at all (see "Lock the origin" below).
The work is done by ngx_http_realip_module. It isn't built by default when you compile nginx yourself (--with-http_realip_module), but the usual distro packages include it. Check:
$ nginx -V 2>&1 | grep -o with-http_realip_module
with-http_realip_module
No output means your build doesn't have it.
The three directives:
set_real_ip_from 173.245.48.0/20; # one line per trusted range, IPv4 and IPv6
real_ip_header CF-Connecting-IP; # which header holds the visitor IP
real_ip_recursive on; # only matters for X-Forwarded-For chains
Put them in the http {} block (on Debian and Ubuntu, a file in /etc/nginx/conf.d/ is included there) and they apply to every server block. Sites on the same box that aren't behind the CDN aren't affected, because their peer address isn't in set_real_ip_from.
After the module swaps the address, $remote_addr is the visitor. The default combined format already logs $remote_addr, so you don't have to touch log_format. The edge address is still available as $realip_remote_addr.
On real_ip_recursive: with it off, nginx takes the last address in the header. With it on, nginx walks the header from the right and stops at the first address that isn't in set_real_ip_from. For X-Forwarded-For you want on. For single-value headers like CF-Connecting-IP it makes no difference.
Apache uses mod_remoteip, which ships with 2.4. Check it's loaded:
$ apachectl -M 2>/dev/null | grep remoteip
remoteip_module (shared)
On Debian and Ubuntu, enable it with a2enmod remoteip. On RHEL-family systems it's usually loaded already; check with httpd -M.
The directives:
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxyList /etc/apache2/cdn-trusted.lst
RemoteIPTrustedProxyList points at a file with one address or CIDR per line (# comments are allowed). You can also write RemoteIPTrustedProxy 173.245.48.0/20 lines directly, but a file is easier to regenerate from cron.
Use RemoteIPTrustedProxy/RemoteIPTrustedProxyList, not RemoteIPInternalProxy. The trusted variant won't accept a private address (10/8, 192.168/16 and so on) as the visitor IP, which is what you want for a public CDN.
Be careful with an empty list. The Apache docs say that without the proxy directives, mod_remoteip trusts every host that sends the header. The cron script further down refuses to write a short list for that reason.
%h vs %a
mod_remoteip changes the client IP Apache uses internally. Whether your log shows it depends on the format string, and the Debian/Ubuntu defaults use %h:
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
%h is the remote hostname field. It prints the IP only because HostnameLookups is off by default. %a is the field the docs tie to mod_remoteip: "Client IP address of the request". On Apache 2.4.39 and later, %h also follows mod_remoteip, but it logs a hostname instead of an address if HostnameLookups is On, and older builds log the proxy. Use %a so the log doesn't depend on either. The connection's own address, the CDN edge, is %{c}a.
Change %h to %a in every format your vhosts use. On Debian and Ubuntu, that's the LogFormat lines in /etc/apache2/apache2.conf:
LogFormat "%v:%p %a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
On RHEL-family systems they're in /etc/httpd/conf/httpd.conf. Also grep your vhosts for any LogFormat or CustomLog with an inline format string:
grep -rn 'LogFormat\|CustomLog' /etc/apache2/ /etc/httpd/ 2>/dev/null | grep '%h'
vhost_combined lines start with host:port, then the client address:
www.example.com:443 203.0.113.9 - - [17/Nov/2026:09:20:11 +0000] "GET /.env HTTP/2.0" 404 196 "-" "curl/8.4.0"
Trunc reads both layouts when the visitor's address is IPv4. If you get IPv6 visitors, send Trunc a combined-format log for now: vhost_combined lines with an IPv6 client address don't parse yet. Don't add fields to the log Trunc reads just for debugging; use a second log (see "Check it worked").
One more side effect: after mod_remoteip, Require ip in Apache checks the visitor's address, not the CDN's. That's good for blocking visitors and useless for locking the origin to the CDN. Use the firewall for that.
nginx, /etc/nginx/conf.d/realip.conf:
include /etc/nginx/cdn-realip.conf; # set_real_ip_from lines, built by the script below
real_ip_header CF-Connecting-IP;
Apache, /etc/apache2/conf-available/remoteip.conf:
RemoteIPHeader CF-Connecting-IP
RemoteIPTrustedProxyList /etc/apache2/cdn-trusted.lsta2enmod remoteip && a2enconf remoteip && apachectl -t && systemctl reload apache2
The ranges come from https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6, one CIDR per line. Include both. Cloudflare can reach your origin over IPv6 if it has an AAAA record.
If you've turned on Cloudflare's Pseudo IPv4 with "Overwrite Headers", CF-Connecting-IP holds a made-up IPv4 address and the real IPv6 one is in CF-Connecting-IPv6. Leave Pseudo IPv4 off if you want real addresses in the log.
First make Fastly-Client-IP trustworthy. Add this to vcl_recv (in the Fastly UI, a VCL snippet of type recv):
if (fastly.ff.visits_this_service == 0 && req.restarts == 0) {
set req.http.Fastly-Client-IP = client.ip;
}
That's the snippet from Fastly's own docs. On the first edge that sees the request, it overwrites whatever the client sent with the address Fastly actually saw. With shielding, the value is kept as the request moves from edge to shield, so your origin still gets the visitor and not the edge POP.
nginx:
include /etc/nginx/cdn-realip.conf;
real_ip_header Fastly-Client-IP;
Apache:
RemoteIPHeader Fastly-Client-IP
RemoteIPTrustedProxyList /etc/apache2/cdn-trusted.lst
Why not X-Forwarded-For? Fastly keeps any X-Forwarded-For the client sent, and its docs say it only adds the client IP on TLS connections. A plain-HTTP request that reaches your origin could carry a forged value and nothing after it. If you'd still rather use X-Forwarded-For, Fastly's docs show the same VCL with one extra line, set req.http.X-Forwarded-For = client.ip;, and then you'd use real_ip_header X-Forwarded-For; with real_ip_recursive on;.
Ranges: https://api.fastly.com/public-ip-list returns JSON with addresses (IPv4) and ipv6_addresses. Fastly announces changes on its status page ahead of time.
Use X-Real-IP. NOC sets it to the visitor's address, one address and nothing else, so there's no chain to walk.
nginx:
include /etc/nginx/cdn-realip.conf; # set_real_ip_from lines for NOC's edge IPs
real_ip_header X-Real-IP;
X-Real-IP is nginx's default real_ip_header, but write it out so the next person doesn't have to know that. real_ip_recursive doesn't matter for a single address.
Apache:
RemoteIPHeader X-Real-IP
RemoteIPTrustedProxyList /etc/apache2/cdn-trusted.lst
The trusted list still matters. Anyone can send an X-Real-IP header; only a request that arrives from a NOC edge should get to set the logged address.
NOC publishes its edge IPs at https://noc.org/ipv4, one per line:
$ curl -s https://noc.org/ipv4 | head -3
144.202.10.148
149.28.222.143
149.28.66.203
They're single IPv4 addresses, not CIDR blocks. set_real_ip_from and RemoteIPTrustedProxyList both take a bare address, so they go in as they are. The same list is in the NOC dashboard under Account Settings > Edge IP Ranges.
If your site is on NOC.org, you can also send NOC's own WAF/CDN logs to Trunc. Open the NOC.org WAF/CDN guide in Trunc's log setup (Settings > Log sources > NOC.org WAF/CDN), copy the key, and paste it in the NOC dashboard under WAF/CDN Settings > Integrations. Those lines are written at the edge, so they already have the visitor's IP.
CDNs add ranges. When they do, requests from the new range get logged with the edge IP again, and you're back to blocking edges. Cloudflare's docs say the list "needs to be updated regularly". Rebuild it from cron.
This one is for Cloudflare and nginx. The Fastly, NOC and Apache changes are below it.
#!/bin/bash
# /usr/local/sbin/update-cdn-ranges
# Rebuilds nginx's trusted CDN list. Keeps the old file if anything looks wrong.
set -euo pipefail
conf=/etc/nginx/cdn-realip.conf
new=$(mktemp)
trap 'rm -f "$new"' EXIT
{
curl -fsS --max-time 60 https://www.cloudflare.com/ips-v4; echo
curl -fsS --max-time 60 https://www.cloudflare.com/ips-v6; echo
} | grep -E '^[0-9a-fA-F:.]+(/[0-9]{1,3})?$' | sed 's/.*/set_real_ip_from &;/' > "$new"
# A short list means a failed or odd download. Don't replace a good file with it.
n=$(wc -l < "$new")
[ "$n" -ge 10 ] || { echo "only $n ranges, not updating" >&2; exit 1; }
cmp -s "$new" "$conf" && exit 0 # nothing changed
[ -f "$conf" ] && cp "$conf" "$conf.bak"
cat "$new" > "$conf"
if ! { nginx -t -q && systemctl reload nginx; }; then
[ -f "$conf.bak" ] && cp "$conf.bak" "$conf"
echo "nginx rejected the new list, restored the old one" >&2
exit 1
fi# /etc/cron.d/update-cdn-ranges
17 4 * * * root /usr/local/sbin/update-cdn-ranges
Run it once by hand before you add the include line, so the file exists. For Apache, run it before you enable remoteip.conf.
For Fastly, swap the { ... } block for:
curl -fsS --max-time 60 https://api.fastly.com/public-ip-list | jq -r '.addresses[], .ipv6_addresses[]'
For NOC.org, swap it for:
curl -fsS --max-time 60 https://noc.org/ipv4
The grep accepts single addresses as well as CIDRs, so NOC's list goes through unchanged. NOC's list is IPv4 only.
For Apache, set conf=/etc/apache2/cdn-trusted.lst and drop the | sed ... part (Apache wants one address or CIDR per line, nothing else). Replace the reload line with if ! { apachectl -t && systemctl reload apache2; }; then (httpd instead of apache2 on RHEL), and change the error message so it says Apache, not nginx.
If you use more than one CDN, put all the ranges in the same file.
A local proxy in front of the web server (HAProxy or Varnish on 127.0.0.1) needs more care. The web server's peer is now always the proxy, so trust 127.0.0.1 as well as the CDN ranges: set_real_ip_from 127.0.0.1; in nginx, RemoteIPInternalProxy 127.0.0.1 in Apache. Have the proxy append the address it saw to X-Forwarded-For. Which header you read then depends on the CDN:
X-Forwarded-For with real_ip_recursive on (Apache walks the list the same way). Cloudflare appends the visitor's address, the proxy appends the edge, and the walk from the right stops at the visitor.set req.http.X-Forwarded-For = client.ip; line in the VCL above. Without it, Fastly passes on whatever X-Forwarded-For the client sent, and on plain HTTP it adds nothing after it, so the walk stops at the forged address.X-Real-IP, and set the proxy to pass that header on only for requests from NOC edge IPs and drop it on everything else. With 127.0.0.1 trusted, the web server believes whatever the proxy forwards.Even with the trusted list in place, anyone who finds your origin IP can skip the CDN and its WAF entirely. Old DNS records, a forgotten subdomain that skips the CDN or the server's own mail headers give it away more often than you'd think. Their requests are logged with their real address, so the log is right, but they're hitting your server with no WAF in front.
Allow 80 and 443 only from the CDN's ranges. First make sure SSH stays allowed from your admin IP, or run ufw allow OpenSSH, before you run ufw enable or delete any rules. Don't delete the 22/tcp rule.
With ufw and Cloudflare, as a script so a failed download stops it before it touches ufw:
#!/bin/bash
# Allow 80/443 from Cloudflare's ranges. Stops if a list fails to download or looks short.
set -euo pipefail
v4=$(curl -fsS --max-time 60 https://www.cloudflare.com/ips-v4)
v6=$(curl -fsS --max-time 60 https://www.cloudflare.com/ips-v6)
[ "$(wc -l <<<"$v4")" -ge 10 ] && [ "$(wc -l <<<"$v6")" -ge 5 ] \
|| { echo "short range list, not touching ufw" >&2; exit 1; }
for net in $v4 $v6; do
ufw allow proto tcp from "$net" to any port 80,443 comment cdn
done
ufw status numbered
Check the output, then delete the old "80/tcp ALLOW Anywhere" style rules by number. For NOC.org, fetch https://noc.org/ipv4 into v4 and drop the IPv6 part; NOC's list is IPv4 only.
Or do the same in your cloud provider's security group, which keeps the traffic off the box completely. Don't add the CDN ranges to your SSH rule. The CDN doesn't proxy SSH, so allowing SSH from edge ranges just opens it to anyone on the CDN.
These rules need the same updates as the trusted list. When a CDN adds a range, a firewall that doesn't know about it drops real traffic from that range.
An IP allowlist only tells you the request came through the CDN, not through your account on it. Another customer of the same CDN can point their own site at your origin IP. To close that, use Cloudflare's Authenticated Origin Pulls, or on Fastly a shared-secret header set in VCL and checked at the origin. NOC.org has an origin auth header for this, NOC-CDN-Auth, which you configure per site in the NOC dashboard.
Module loaded:
apachectl -M 2>/dev/null | grep remoteip
nginx -V 2>&1 | grep -o with-http_realip_module
Config parses, then reload:
apachectl -t && systemctl reload apache2
nginx -t && systemctl reload nginx
From a machine outside your network (a laptop on a hotspot, another VPS), note its public address and make a request you can find in the log:
$ curl -s https://ifconfig.me; echo
198.51.100.23
$ curl -s -o /dev/null -w '%{http_code}\n' 'https://www.example.com/?realip-test=1'
200
On the web server:
$ grep 'realip-test=1' /var/log/nginx/access.log | tail -1
198.51.100.23 - - [17/Nov/2026:10:02:44 +0000] "GET /?realip-test=1 HTTP/2.0" 200 18211 "-" "curl/8.4.0"
For Apache on Debian/Ubuntu, vhost_combined traffic usually goes to /var/log/apache2/other_vhosts_access.log; check your CustomLog lines.
Now try to forge it:
curl -s -o /dev/null -w '%{http_code}\n' \
-H 'X-Forwarded-For: 203.0.113.66' -H 'X-Real-IP: 203.0.113.66' \
-H 'CF-Connecting-IP: 203.0.113.66' -H 'Fastly-Client-IP: 203.0.113.66' \
'https://www.example.com/?spoof-test=1'
grep spoof-test should show 198.51.100.23 again, never 203.0.113.66. If the CDN refuses the request outright, that's a pass too.
And go around the CDN, straight to the origin (192.0.2.10 here):
curl -sv --connect-timeout 5 -o /dev/null --resolve www.example.com:443:192.0.2.10 https://www.example.com/
With the origin locked, that times out.
If something's off, log both addresses side by side in a separate file for an hour:
# nginx, in http {}
log_format realip_debug '$remote_addr via $realip_remote_addr xff="$http_x_forwarded_for"';
access_log /var/log/nginx/realip-debug.log realip_debug;# Apache
LogFormat "%a via %{c}a xff=\"%{X-Forwarded-For}i\"" realip_debug
CustomLog /var/log/apache2/realip-debug.log realip_debug
A line like 172.70.34.112 via 172.70.34.112 means the edge isn't in your trusted list. 198.51.100.23 via 172.70.34.112 is what you want. Remove the debug log when you're done.
Once the log has real visitor addresses, Trunc's Active Response list will have real attacker addresses on it. But think about where you enforce it.
The ipset script blocks at the origin firewall, by source address. Behind a CDN, the attacker never connects to your origin. The CDN does. Blocking the attacker's IP in iptables does nothing for web traffic. It still works for SSH and anything else that isn't going through the CDN.
For web traffic, block in the web server, which sees the real address after realip/remoteip. nginx's geo module checks $remote_addr by default, which is the visitor at that point:
# in http {}
geo $trunc_block {
default 0;
include /etc/nginx/trunc-block.geo; # lines like: 203.0.113.9 1;
}
# in each server {} behind the CDN
if ($trunc_block) { return 403; }
Build the file from your blocklist URL (the same one the ipset script uses). Run this once by hand before you add the geo block, so the file exists, then from cron:
#!/bin/bash
# /usr/local/sbin/trunc-block-nginx
# Rebuilds nginx's geo file from the Trunc blocklist. Keeps the old file if anything fails.
set -euo pipefail
url='https://<your-endpoint>/security-responses/ipv4-blocklist/<your-list-id>.txt'
geo=/etc/nginx/trunc-block.geo
new=$(mktemp "$geo.XXXXXX")
trap 'rm -f "$new"' EXIT
list=$(curl -fsS --connect-timeout 10 --max-time 30 "$url") # failed or stalled download: stop here
printf '%s\n' "$list" | tr -d '\r' \
| grep -E '^((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])$' \
| sed 's/$/ 1;/' > "$new" || [ $? -eq 1 ] # 1 = no addresses: empty file, nothing blocked
cmp -s "$new" "$geo" && exit 0 # nothing changed
[ -f "$geo" ] && cp "$geo" "$geo.bak"
chmod 644 "$new"
mv "$new" "$geo"
if ! { nginx -t -q && systemctl reload nginx; }; then
[ -f "$geo.bak" ] && cp "$geo.bak" "$geo"
echo "nginx rejected the new blocklist, restored the old one" >&2
exit 1
fi# /etc/cron.d/trunc-block-nginx
* * * * * root flock -n /run/trunc-block-nginx.lock /usr/local/sbin/trunc-block-nginx
If the download fails, the script stops and the old file stays. If the download works and the list is empty, the file is emptied, because an empty Active Response list means there's nothing to block. It only reloads nginx when the list changed, and if nginx -t rejects the new file, the old one goes back. curl gives up after 30 seconds, and flock -n in the cron line skips a run while the previous one is still going.
Apache can do it without a reload. A txt RewriteMap is re-read when the file's mtime changes, and %{REMOTE_ADDR} is the mod_remoteip address:
# in the <VirtualHost>, not .htaccess (RewriteMap can't be declared there)
RewriteEngine on
RewriteMap trunc_block "txt:/etc/apache2/trunc-block.map"
RewriteCond ${trunc_block:%{REMOTE_ADDR}|0} =1
RewriteRule ^ - [F]
The map file is one address 1 pair per line. Same script, minus the nginx parts, and run it once before you add the RewriteMap lines:
#!/bin/bash
# /usr/local/sbin/trunc-block-apache
set -euo pipefail
url='https://<your-endpoint>/security-responses/ipv4-blocklist/<your-list-id>.txt'
map=/etc/apache2/trunc-block.map
new=$(mktemp "$map.XXXXXX")
trap 'rm -f "$new"' EXIT
list=$(curl -fsS --connect-timeout 10 --max-time 30 "$url")
printf '%s\n' "$list" | tr -d '\r' \
| grep -E '^((25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])$' \
| sed 's/$/ 1/' > "$new" || [ $? -eq 1 ] # 1 = no addresses: empty map, nothing blocked
cmp -s "$new" "$map" && exit 0
chmod 644 "$new"
mv "$new" "$map" # picked up on the next request, no reload# /etc/cron.d/trunc-block-apache
* * * * * root flock -n /run/trunc-block-apache.lock /usr/local/sbin/trunc-block-apache The list is IPv4 only, so IPv6 visitors won't be on it. Blocked requests still cross the CDN to reach you, but they get a 403 from your server instead of a page. If your CDN has its own IP block rules, you can push the same list there instead, which stops the request at the edge.
Once each line has the visitor's address (in a combined log if you get IPv6 visitors, as above), the rest works the way it does for a site with no CDN:
There's more on reading the logs themselves in our nginx and Apache guides.
We love logs. In this section we will share some articles from our team to help you get better at logging.
Logging for fun and a good night of sleep.
Latest articles from our learning center.
Do you have an idea for an article that is not here? See something wrong? Contact us at support@noc.org
14 days free trial. No credit card required.