php – How to set maintenance mode for Laravel 5.8 behind load balancer in EC2 instance and access from my office?-ThrowExceptions

Exception or error:

I want to use Laravel maintenance mode on EC2 instance behind the load balancer because I do not want to touch AWS console for returning maintenance content.

Moreover, I want to access my app via web browser from my office while maintenance mode.

I did following and it turns into maintenance mode.

But, I can not see my app from my office although the IP at my office in the allow list.

php artisan down --allow=127.0.0.1 --allow=myip/34

Do you have any suggestions for this?

Here is my environment information-
PHP: 5.7
Laravel: 5.8

Also, I have following source code in App/Http/Middleware/TrustProxies.php


class TrustProxies extends Middleware
{
protected $proxies = '*';
protected $headers = [
Request::HEADER_FORWARDED => 'FORWARDED',
Request::HEADER_X_FORWARDED_FOR => 'X_FORWARDED_FOR',
Request::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
Request::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
Request::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
];
}

Regards,

How to solve:

Since you’re behind a load balancer you’ll be receiving the ip of that load balancer rather than the client ip.

TL;DR

In your app/Http/Middleware/TrustProxies.php change the protected $proxies; line to be:

protected $proxies = '*';

Since Laravel 5.4, there is an out-of-the-box way of dealing with this: TrustedProxy. If you’re using an earlier version of Laravel you can still use the package, however, you’ll have to install it yourself.

Where possible, you should try and set the ip addresses of the reverse proxy, however, this isn’t possible with AWS since the ip addresses of the load balancer change all the time (source: https://github.com/fideloper/TrustedProxy/wiki/IP-Addresses-of-Popular-Services#aws-elastic-load-balancers).

For more information, you can refer to the Laravel documentation Configuring Trusted Proxies or the Github page for the underlying package.

Leave a Reply

Your email address will not be published. Required fields are marked *