Skip to content

Commit 2c3edc2

Browse files
committed
Add instructions for restoring original visitor IPs when using Caddy
1 parent 943f8fc commit 2c3edc2

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
@@ -454,6 +454,42 @@ clientIPDetection:
454454

455455
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).
456456

457+
### Caddy
458+
459+
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](https://developers.cloudflare.com/fundamentals/reference/http-request-headers/#cf-connecting-ip).
460+
461+
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:
462+
```txt title="Caddyfile"
463+
https://example.com {
464+
reverse_proxy localhost:8080 {
465+
# Sets X-Forwarded-For as the value Cloudflare gives us for CF-Connecting-IP.
466+
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
467+
}
468+
}
469+
```
470+
471+
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:
472+
```txt title="Caddyfile"
473+
https://example.com {
474+
# Restrict access to Cloudflare IPs (https://www.cloudflare.com/ips/)
475+
@cloudflare {
476+
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
477+
}
478+
479+
# Process requests from Cloudflare IPs
480+
handle @cloudflare {
481+
reverse_proxy localhost:8080 {
482+
# Sets X-Forwarded-For as the value Cloudflare gives us for CF-Connecting-IP.
483+
header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
484+
}
485+
}
486+
487+
# Deny requests from non-Cloudflare IPs
488+
handle {
489+
respond "Access Denied" 403
490+
}
491+
}
492+
```
457493
---
458494

459495
## Related Resources

0 commit comments

Comments
 (0)