Member-only story

Configuring Proxy Trust in Laravel for Secure Deployments

I Nyoman Jyotisa
2 min readJan 17, 2025

--

When deploying Laravel applications behind load balancers or reverse proxies, it’s crucial to configure the TrustProxies middleware. This ensures accurate detection of client information, such as IP addresses, and proper handling of HTTPS protocols.

Here’s an example of basic proxy configuration in Laravel:

use Illuminate\Http\Request;

// Basic proxy configuration
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: [
'10.0.0.0/8',
'172.16.0.0/12'
]);
});

Configuring for Cloud Environments

Let’s explore a practical example of tailoring the TrustProxies middleware for different environments, such as production, staging, or local development:

use Illuminate\Http\Request;

->withMiddleware(function (Middleware $middleware) {
// Detect the current environment
$environment = env('APP_ENV');
switch ($environment) {
case 'production':
// Configuration for AWS Elastic Load Balancers
$middleware->trustProxies(
at: '*',
headers: Request::HEADER_X_FORWARDED_AWS_ELB
);
break;
case 'staging':
// Configuration for DigitalOcean or…

--

--

Responses (1)