Laravel behind Coolify: fixing HTTPS and trusted proxies
When Laravel sits behind a reverse proxy that terminates SSL (Coolify, Traefik, Nginx, Cloudflare), it generates HTTP URLs and breaks in production. Here's the one-line fix and why it matters.
When you deploy a Laravel app behind Coolify (or any reverse proxy that terminates SSL, such as Traefik, Nginx proxy or Cloudflare), you’ll hit mixed-content warnings: assets loaded over HTTP inside an HTTPS page.
The fix
In bootstrap/app.php:
->withMiddleware(function (Middleware $middleware): void {
$middleware->trustProxies(at: '*');
// ...
})
Why
The proxy forwards requests to your Laravel app over HTTP internally. Without trustProxies, Laravel ignores the X-Forwarded-Proto and X-Forwarded-For headers and generates all URLs (assets, redirects, routes) in HTTP. Trust the proxy and Laravel will honor the original scheme.
How to check it worked
Deploy, then check from outside and from inside the app.
From outside, ask for a URL that redirects (for example a page behind the auth middleware) and look at the Location header:
curl -sI https://your-app.example.com/dashboard | grep -i '^location'
Before the fix it points to http://your-app.example.com/login; after the fix it starts with https://. Then look for any absolute http:// link left in the HTML of a page:
curl -s https://your-app.example.com/ | grep -o 'http://your-app\.example\.com[^"]*'
No output means every asset and route URL is now built over HTTPS.
From inside, remember that X-Forwarded-Proto is a header the proxy adds on its way to Laravel: you will not see it in the curl response. To see what the app receives, add a temporary route and remove it after the check:
// routes/web.php (temporary)
Route::get('/proxy-check', fn (Illuminate\Http\Request $request) => [
'forwarded_proto' => $request->header('X-Forwarded-Proto'),
'scheme' => $request->getScheme(),
'secure' => $request->secure(),
'url' => url('/'),
]);
Behind Coolify you want forwarded_proto set to https, scheme to https, secure to true and a url starting with https://. If forwarded_proto is https but scheme is still http, the proxy is not trusted yet: check that the change is really in the build Coolify deployed.
Laravel 10 and older
bootstrap/app.php with withMiddleware() exists from Laravel 11. On older versions the same setting lives in the middleware App\Http\Middleware\TrustProxies, already registered in the global $middleware stack of app/Http/Kernel.php in a standard install.
On Laravel 9 and 10 the middleware extends the class shipped with the framework:
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;
class TrustProxies extends Middleware
{
protected $proxies = '*';
protected $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;
}
From Laravel 5.5 to 8 the file looks the same, but the parent class comes from the fideloper/proxy package: the first use line is use Fideloper\Proxy\TrustProxies as Middleware;, and older skeletons set $headers to Request::HEADER_X_FORWARDED_ALL, which also covers the proto header. In every version the fix is the $proxies = '*' line: by default the property is empty, so no proxy is trusted.
When you upgrade one of these projects to Laravel 9, fideloper/proxy goes away: the official upgrade guide tells you to remove it from composer.json, switch the use line to Illuminate\Http\Middleware\TrustProxies and replace HEADER_X_FORWARDED_ALL with the list of headers shown above. A project created on Laravel 11 or later has no such file: the setting is the trustProxies() call at the top of this post.
When to apply
On any Laravel project deployed behind a proxy that terminates SSL. Add it as a standard step in your production setup.
'*' trusts whatever machine sends the request. That is fine when the app can only be reached through the proxy, which is the usual Coolify setup: the container port is not published and Traefik is the only way in. If the app port is also open to the internet, anyone could send a fake X-Forwarded-Proto or X-Forwarded-For: close the port, or list the proxy addresses instead of '*'.
A short call to understand your context, constraints, and what we'd be building. No slide deck, no sales script, just a technical conversation. If we're a fit, we move forward. If not, I'll point you to someone who is.
Get in touch