Skip to content

Commit 008b0b2

Browse files
dannyfergusondcpena
authored andcommitted
Add instructions for restoring original visitor IPs when using Caddy (cloudflare#19008)
* Add instructions for restoring original visitor IPs when using Caddy * Fixed cross reference URL --------- Co-authored-by: Denise Peña <[email protected]>
1 parent db6ffff commit 008b0b2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/content/docs/support/troubleshooting/restoring-visitor-ips/restoring-original-visitor-ips.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,42 @@ clientIPDetection:
455455

456456
For more details, refer to [Custom header original IP detection extension](https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/http/original_ip_detection/custom_header/v3/custom_header.proto).
457457

458+
### Caddy
459+
460+
If you are running an application behind [Caddy](https://caddyserver.com/) that relies on the `X-Forwarded-For` header, you can configure Caddy to override the header with Cloudflare's [CF-Connecting-IP header](/fundamentals/reference/http-headers/#cf-connecting-ip).
461+
462+
It is advised that you also only accept traffic from [Cloudflare's IP addresses](https://www.cloudflare.com/ips/); otherwise, the header could be spoofed. That's why, in the second example, we handle this as part of the Caddy configuration. Alternatively, you can handle this at the firewall level, which is usually easier to automate. If you already have a firewall or other measure in place to ensure this, your Caddyfile could look like this:
463+
```txt title="Caddyfile"
464+
https://example.com {
465+
reverse_proxy localhost:8080 {
466+
# Sets X-Forwarded-For as the value Cloudflare gives us for CF-Connecting-IP.
467+
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
468+
}
469+
}
470+
```
471+
472+
If you want Caddy to handle only accepting traffic from [Cloudflare's IP addresses](https://www.cloudflare.com/ips/), you can use a configuration like this one:
473+
```txt title="Caddyfile"
474+
https://example.com {
475+
# Restrict access to Cloudflare IPs (https://www.cloudflare.com/ips/)
476+
@cloudflare {
477+
remote_ip 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22 141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20 197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/13 104.24.0.0/14 172.64.0.0/13 131.0.72.0/22 2400:cb00::/32 2606:4700::/32 2803:f800::/32 2405:b500::/32 2405:8100::/32 2a06:98c0::/29 2c0f:f248::/32
478+
}
479+
480+
# Process requests from Cloudflare IPs
481+
handle @cloudflare {
482+
reverse_proxy localhost:8080 {
483+
# Sets X-Forwarded-For as the value Cloudflare gives us for CF-Connecting-IP.
484+
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
485+
}
486+
}
487+
488+
# Deny requests from non-Cloudflare IPs
489+
handle {
490+
respond "Access Denied" 403
491+
}
492+
}
493+
```
458494
---
459495

460496
## Related Resources

0 commit comments

Comments
 (0)