
When deploying a Laravel application behind a reverse proxy—such as Nginx, a Docker container gateway, an AWS Application Load Balancer, or Cloudflare—your site may work locally over HTTP and immediately fail in production with one of two errors:
ERR_TOO_MANY_REDIRECTS
example.com redirected you too many times.
Or your HTML loads over HTTPS, but every CSS, JavaScript, and pagination link breaks in the browser console with:
Mixed Content: The page at 'https://example.com/login' was loaded over HTTPS,
but requested an insecure stylesheet 'http://example.com/build/assets/app.css'.
This request has been blocked; the content must be served over HTTPS.
Both issues stem from the same root cause: TLS encryption terminates at the proxy or CDN edge, and the upstream PHP-FPM container receives plain HTTP traffic without knowing the original visitor connected over HTTPS.
Quick Diagnostic Reference
| Symptom | Root Cause | Solution |
|---|---|---|
Browser shows ERR_TOO_MANY_REDIRECTS immediately after enabling Cloudflare |
Cloudflare SSL/TLS mode is set to Flexible, sending HTTP requests to an origin that redirects to HTTPS | Switch Cloudflare SSL/TLS mode to Full (strict) (Step 1) |
asset(), route(), and Vite links render as http:// on an https:// page |
Laravel ignores X-Forwarded-Proto because the reverse proxy IP is untrusted |
Configure $middleware->trustProxies() in bootstrap/app.php (Step 2) |
Laravel redirects https:// requests back to https:// infinitely |
Reverse proxy overwrites or strips X-Forwarded-Proto before passing traffic to PHP-FPM |
Forward $http_x_forwarded_proto or $scheme in Nginx (Step 3) |
Login form loops back to /login or throws 419 Page Expired after HTTPS redirect |
SESSION_SECURE_COOKIE=true fails when $request->secure() evaluates to false
|
Enforce URL::forceScheme('https') and verify .env session settings (Step 4) |
Step 1: Fix Cloudflare "Flexible" SSL Redirect Loops
If your domain sits behind Cloudflare and throws ERR_TOO_MANY_REDIRECTS the moment you enable HTTPS, check your Cloudflare SSL mode before touching application code:
- Open your Cloudflare Dashboard and navigate to SSL/TLS -> Overview.
- If the encryption mode is set to Flexible, Cloudflare accepts HTTPS from visitors but connects to your origin server over unencrypted HTTP (port 80).
- When your Nginx server or Laravel app sees port 80 and responds with a
301redirect tohttps://, Cloudflare catches the redirect and sends another HTTP request to port 80—creating an infinite loop. - Change the encryption mode from Flexible to Full (strict) (and install a free Cloudflare Origin Certificate on your server if you do not already have a valid TLS certificate).
You can verify whether a redirect loop is coming from your edge proxy or Laravel using curl:
curl -I -L --max-redirs 5 https://example.com
Inspect the Location: and Server: headers in each hop to see which layer is issuing the repeated 301 or 302 response.
Step 2: Configure trustProxies in Laravel (bootstrap/app.php)
By default, Laravel ignores X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto headers so attackers cannot spoof their IP address or scheme. However, inside Docker networks, Kubernetes clusters, or behind cloud load balancers, PHP-FPM only sees the internal proxy IP—causing $request->isSecure() to return false.
Open bootstrap/app.php and configure trustProxies inside withMiddleware():
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\Request;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(
at: '*',
headers: Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB
);
})
->withExceptions(function (Exceptions $exceptions): void {
//
})->create();
Locking Down at: to Specific Docker or Load Balancer Subnets
If your server is directly reachable without a firewall blocking non-proxy traffic, replace at: '*' with your private Docker bridge or load balancer CIDR ranges so external clients cannot forge forwarded headers:
$middleware->trustProxies(at: [
'10.0.0.0/8',
'172.16.0.0/12',
'192.168.0.0/16',
]);
Step 3: Forward X-Forwarded-Proto Properly in Nginx
If you run an Nginx reverse proxy in front of an app container (or behind Cloudflare), make sure Nginx preserves the original scheme instead of overwriting it with http.
Inside your Nginx location / block, pass $http_x_forwarded_proto when an upstream CDN is present, falling back to $scheme:
map $http_x_forwarded_proto $proxy_x_forwarded_proto {
default $http_x_forwarded_proto;
'' $scheme;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://laravel_app:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Port $server_port;
}
}
Reload Nginx to apply the header mapping:
sudo nginx -t && sudo systemctl reload nginx
Step 4: Force HTTPS URL Generation in AppServiceProvider
If third-party packages, pagination links ($users->links()), or signed URLs still emit http:// links in production, enforce HTTPS scheme generation inside app/Providers/AppServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
if ($this->app->environment('production')) {
URL::forceScheme('https');
}
}
}
Confirm your production .env file also uses https:// for APP_URL:
APP_ENV=production
APP_URL=https://example.com
SESSION_SECURE_COOKIE=true
After updating .env or bootstrap/app.php, clear and rebuild your cached configuration:
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
Top comments (0)