monicahq/laravel-cloudflare
Laravel middleware that automatically trusts Cloudflare proxy IP ranges so client IPs and forwarded headers are handled correctly. Drop-in replacement for Laravel’s TrustProxies, with an optional callback to customize how proxy lists are loaded.
TrustProxies middleware to dynamically include Cloudflare IP ranges as trusted proxies, ensuring accurate client IP detection behind Cloudflare. This aligns perfectly with Laravel applications using Cloudflare for CDN, security, or DDoS protection.TrustProxies middleware, which is a clean architectural approach.LaravelCloudflare::getProxiesUsing(). This allows TPMs to adapt the package for niche requirements without forking.bootstrap/app.php. No database migrations or complex configurations are needed.fideloper/proxy package with Laravel’s native TrustProxies, reducing external dependencies. No additional services (e.g., queues, jobs) are required.cloudflare:reload via Laravel’s task scheduler.TrustProxies globally affects all requests. If other middleware relies on the original TrustProxies behavior (e.g., custom proxy logic), conflicts may arise. Validation Required: Test with existing middleware (e.g., ThrottleRequests, GeoBlock).filter_var with FILTER_VALIDATE_IP).trusted_proxies?request()->ip(), ThrottleRequests) that must align with Cloudflare’s CF-Connecting-IP header? If so, test thoroughly.TrustProxies if Cloudflare IPs fail to load? (Configurable via laravelcloudflare.enabled.)TrustProxies errors, track CF-Connecting-IP vs. REMOTE_ADDR mismatches.)CF-Connecting-IP). Not suitable for other CDNs (e.g., CloudFront, Fastly) without modification.trusted_proxies in config/app.php or .env (e.g., TRUSTED_PROXIES="192.168.1.0/24").TrustProxies (e.g., custom IP validation logic).composer require monicahq/laravel-cloudflare
TrustProxies middleware in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) {
$middleware->replace(
\Illuminate\Http\Middleware\TrustProxies::class,
\Monicahq\Cloudflare\Http\Middleware\TrustProxies::class
);
})
php artisan vendor:publish --provider="Monicahq\Cloudflare\TrustedProxyServiceProvider"
request()->ip() returns CF-Connecting-IP for Cloudflare requests.ThrottleRequests, geo-blocking) with Cloudflare traffic.cloudflare:reload in routes/console.php:
Schedule::command('cloudflare:reload')->daily();
TrustProxies errors or IP mismatches.4.x for Laravel 13). Avoid mixing branches.TrustProxies behavior. Test with:
ThrottleRequestsGeoBlock (e.g., spatie/laravel-geo-blocking)TrustProxies if issues arise..env:
LARAVEL_CLOUDFLARE_ENABLED=false
cloudflare:reload daily or post-deployment.artisan cloudflare:view for IP changes or use Cloudflare’s API webhooks to trigger updates.php artisan cloudflare:reload manually or adjust the cache TTL in config/laravelcloudflare.php.TrustProxies errors. Verify Cloudflare headers (CF-Connecting-IP) are present.request()->ip() with request()->header('CF-Connecting-IP') in logs.allow_url_fopen is enabled for IP fetching (default: uses Laravel’s HTTP client).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Cloudflare IP fetch fails | Stale I |
How can I help you explore Laravel packages today?