# Cloudflare Plugin (Allow only CF-Traffic to your Server) ### [](https://github.com/agence-gaya/traefik-plugin-cloudflare "https://github.com/agence-gaya/traefik-plugin-cloudflare") [](https://plugins.traefik.io/plugins/65a1d28f0f0494247310c69d/cloudflare "https://plugins.traefik.io/plugins/65a1d28f0f0494247310c69d/cloudflare") ### ### ### Overview This plugin ensures that incoming requests must originate from Cloudflare’s network (or other CIDRs that you explicitly allow). It is particularly useful when you only want Cloudflare-proxied traffic to reach your services. By using Cloudflare’s IP ranges, the plugin can block all other sources of traffic and help enhance security. ##### [Plugin Page](https://plugins.traefik.io/plugins/65a1d28f0f0494247310c69d/cloudflare) ### Requirements - A working Traefik setup (for instance, [ Traefik Reverse Proxy for Docker Swarm ](https://wiki.aeoneros.com/books/traefik-reverse-proxy-for-docker-swarm)). - A valid **DNS-01 Challenge** configuration with Cloudflare to manage certificates (see [ Docker Compose with Let's Encrypt DNS Challenge ](https://wiki.aeoneros.com/books/traefik-reverse-proxy-for-docker-swarm/page/docker-compose-with-lets-encrypt-dns-challenge-cloudflare-recommended)).
With these prerequisites in place, you can integrate the Cloudflare plugin to filter and rewrite traffic so that only Cloudflare IP ranges can access your services through Traefik.
### Features - 🌐 Only allow traffic originating from Cloudflare IP v4 and v6 - 🛡️ Custom CIDRs list can be added to allow requests not from Cloudflare - ♻️ Refresh Cloudflare CIDRs from the [Cloudflare API](https://api.cloudflare.com/client/v4/ips) - ⚙️ Handle `X-Forwarded-For` original header to allow Cloudflare requests from a trusted reverse proxy behind Traefik - 🛠️ Rewrite requests `X-Forwarded-For` header with the user IP provided by `CF-Connecting-IP` - 🌎 Rewrite requests `X-Forwarded-Proto` header with the scheme provided by `CF-Visitor` - 📡 Rewrite requests `X-Real-IP` header with the user IP provided by `CF-Connecting-IP` - 🔒 Rewrite `RemoteAddress` to permit Traefik *ipwhitelist* middleware to work on IP provided by `CF-Connecting-IP` ### Configuration #### Plugin OptionsKey | Type | Default | Description |
---|---|---|---|
`trustedCIDRs` | `[]string` | `[]` | Requests coming from a source not matching any of these CIDRs will be terminated with a 403. If empty, it is populated with Cloudflare’s CIDRs. |
`allowedCIDRs` | `[]string` | `[]` | Requests coming from a source matching any of these CIDRs will not be terminated with a 403 and no overwrite of request header append. |
`refreshInterval` | `time.Duration` | `24h` | When `trustedCIDRs` is empty, Cloudflare’s CIDRs will be refreshed after this duration. Using a value of 0 seconds disables the refresh. |
`overwriteRequestHeader` | `bool` | `true` | When `true`, the request’s header is rewritten. When `false`, any header or Traefik `RemoteAddress` is modified, filtering only the request from Cloudflare IP. |
`appendXForwardedFor` | `bool` | `false` | Works only when `overwriteRequestHeader` is `true`. When `true`, prepend Cloudflare IP to `X-Forwarded-For` instead of replacing the first value. |
`debug` | `bool` | `false` | Output debug messages in Traefik logs. |