Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlAdler's avatar

Configuring different TurstProxies per environment

I need to have different TurstProxies configuration in different environments. I'm having trouble coming up with a good solution for this.
TrustProxies are configured in bootstrap/app.php, but .env variables are not available yet there, so you cannot have an environment variable to check and act accordingly. Does anyone have any ideas on how this can be solved?

0 likes
6 replies
LaryAI's avatar
Level 58

To configure different TrustProxies settings per environment in a Laravel application, you can leverage the App\Http\Middleware\TrustProxies middleware. This middleware is executed after the environment variables are loaded, allowing you to use .env variables to configure your trusted proxies.

Here's how you can achieve this:

  1. Modify the TrustProxies Middleware:

    Open the App\Http\Middleware\TrustProxies class. You can customize the $proxies property to use environment variables.

    namespace App\Http\Middleware;
    
    use Illuminate\Http\Middleware\TrustProxies as Middleware;
    use Illuminate\Http\Request;
    
    class TrustProxies extends Middleware
    {
        /**
         * The trusted proxies for this application.
         *
         * @var array|string|null
         */
        protected $proxies;
    
        /**
         * The headers that should be used to detect proxies.
         *
         * @var int
         */
        protected $headers = Request::HEADER_X_FORWARDED_ALL;
    
        public function __construct()
        {
            // Set the proxies based on the environment
            $this->proxies = config('trustedproxies.proxies');
        }
    }
    
  2. Create a Configuration File:

    Create a new configuration file named trustedproxies.php in the config directory. This file will hold the proxy settings for different environments.

    return [
        'proxies' => env('TRUSTED_PROXIES', null),
    ];
    
  3. Set Environment Variables:

    In your .env files for different environments, set the TRUSTED_PROXIES variable. You can specify a comma-separated list of IP addresses or use * to trust all proxies.

    For example, in your .env file for the production environment:

    TRUSTED_PROXIES=192.168.1.1,192.168.1.2
    

    And in your local .env file:

    TRUSTED_PROXIES=*
    
  4. Clear Configuration Cache:

    If you have configuration caching enabled, make sure to clear the cache to apply the changes:

    php artisan config:cache
    

By following these steps, you can configure different TrustProxies settings for each environment using environment variables, ensuring that your application correctly handles proxy headers based on the environment it is running in.

AlAdler's avatar

@LaryAI I'm using Laravel 11 so there is no App\Http\Middleware\TrustProxies, it's now hidden inside the vendor directory in Illuminate\Http\Middleware\TrustProxies.

AlAdler's avatar

If anybody is interested, what I ended up doing is making my own TrustProxies middleware that extends Illuminate\Http\Middleware\TrustProxies . Inside the new middleware we have access to config so we can do whatever we want. In bootstrap/app.php get the global middlewares array, replace the Illuminate\Http\Middleware\TrustProxies with my own, and then set the global middlewares with the new list.

AlAdler's avatar

@martinbean For example, if you want evaluate Laravel Cloud vs Forge+AWS (with ELB). With that later you need a specific set of trusted proxies while in Laravel Cloud you do not.

n_jain's avatar

Thank you so muc bro ! I was stuck on this for months. Here is what works for me

  ->withMiddleware(function (Middleware $middleware) {
        $middleware->replace(
               "Illuminate\Http\Middleware\TrustProxies",  
               "App\Http\Middleware\TrustProxies"
        );
    })

Please or to participate in this conversation.