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

squibby's avatar

How to redirect http to https when behind load balancer

I have setup up 2 custom EC2 severs on AWS then used forge to provision them. I have setup elastic load balancer on AWS and successfully balance the traffic over http and https to the 2 app servers.

How can I force the browser to https if http is being used. Do I need to manually login and setup some kind of redirect? I have tried updating the nginx.conf file with a redirect but everything broke.

Another issue is, usually if I provision a server from within forge itself (not custom vps) I then name my site the name of the url and everything works out. Using the Elastic load balancer I can only get everything to work if use 'default' as the site name. Is there any way to change this? Or does it not matter if I just use 'default' so long as it works.

Is the fideloper/proxy package, something that will fix these issues?

Has anybody any experience of performing such a task. Would greatly appreciate some info.

Thanks

0 likes
1 reply
squibby's avatar

So instead of setting up a redirect in the nginx.conf file, I set it up in /etc/nginx/sites-enabled/default file.

if($http_x_forwarded_proto = 'http'){
    return 301 https://mydomain.com$request_uri
}

This seemed to do what I wanted and force everything over to https.

Laravel forge recommends to delete the 'default' site and create a site with the domain name. I have done this previously and it works fine. However when doing this the site is no longer available via the ip address only in the browser. It only works after pointing DNS to the server IP and then it works. This is fine and has suited me for single server deployments.

Now I have 2 servers behind a load balancer, I am finding that the AWS elastic load balancer health checks fail if I dont use 'default site'. The reason I assume is that when the Load balancer pings the ip it doesnt receive a 200 response and assumes the servers are not there.

So.... is it cool to keep the app running in the 'default' site... or should I be doing it in a different way.

Thanks.

Please or to participate in this conversation.