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

fenos's avatar

Routes https on loader balance

Hi guys, I have a Load balancer running on HTTPS and behind we are running the communication in normal HTTP (between the balancer and the APP servers).

Using the https option on a router group as the following:

$router->group(['https'], function ($router)
{
require app_path('Http/routes.php');
});

it apply correctly the prefix https to all the url. When i try to navigate on the site, on every url I receive a 404 status code and I'm not able to see the pages using this filter.

Do you have any suggestion guys or why it behave like so?

0 likes
5 replies
fideloper's avatar
Level 11

Yep, find out what headers the load balancer sends to PHP (dump out your $_SERVER global array) and set the Trusted Proxy configuration (part of the underlying Symfony HTTP classes).

This package makes putting Laravel behind a load balancer easier, and explains what's going on.

You can use the latest version 2.* if you're on Laravel 4, else use the latest tag (3.* version) for Laravel 5.

Lastly, this article goes more in depth with issues you might come across when putting any application behind a load balancer.

Once that's configured, I believe you can skip over configuring the "https" group, since Laravel should generate redirects and URLs/links correctly based on the headers it receives from the load balancer. If you have control over the load balancer, make sure it passes along some X-Forwarded-* headers for the application to use.

4 likes
fenos's avatar

Thanks so much @fideloper, you save me a lots time and stress :))) it works amazing!! My only concern is, I'm using amazon load balancer, I installed your package and set up as a trusted proxy the DNS entry point of the balancer as the following:

 'proxies' => [
        'mybalancerentrypoint.elb.amazonaws.com',
    ],

I done in this way for the suggestion of amazon it self on this note:

Note: Because the set of IP addresses associated with a LoadBalancer can change over time, you should never create an "A" record with any specific IP address. If you want to use a friendly DNS name for your load balancer instead of the name generated by the Elastic Load Balancing service, you should create a CNAME record for the LoadBalancer DNS name, or use Amazon Route 53 to create a hosted zone.

is it a correct way to set up the proxy and it's secure enough?

also dumping the $_SERVER global variable i seen the headers sent from the balancer as the same of your default headers in the config file :))

fideloper's avatar

Cool!

Is the X-Forwarded-For header (HTTP_X_FORWARDED_FOR in the $_SERVER global) an IP address or a hostname? That's what the package/Symfony classes have to go by, and what needs to be matched in the proxies configuration.

You may have to use 'proxies' => * if you get an IP address rather than 'mybalancerentrypoint.elb.amazonaws.com'.

1 like
solutionswide's avatar

@fideloper Although I have your package working for normal requests, the trusted proxies seem to get ignored when the app is in maintenance mode. Have you come across a similar issue?

I'm using laravel 5.1 on AWS with an ELB that receives requests on port 443 but sends them to the EC2 instances over port 80

Update

After digging deeper, I figured out that the trusted proxies are set correctly, but during maintenance mode, the laravel helper functions are incorrectly setting the protocol.

/* app/Exceptions/Handler.php */
...
public function render($request, Exception $e)
{
    dd(asset('photo.jpg'));
}

When I visit https://mypage.com/page-with-error

If anyone has in insight as to why this happens, please let me know!

Chrizzmeister's avatar

@SOLUTIONSWIDE - Sorry for digging this up but @solutionswide problem is related to the order in the middleware stack. CheckForMaintance middleware happens before TrustedProxy so i think that moving TrustedProxy to the top of the stack solves this issue.

Please or to participate in this conversation.