4

I have configured the load balancer to route the request to two of Ec2 Instance running a NodeJs server. I need to direct the request coming from both http (port 80) and https (port 443) to http (port 80) of the EC2 instances in NodeJs. I have uploaded the ssl certificate to AWS and configured the load balancer to use ssl certificate. The problem is the request coming from http port doesn't automatically route to https. It has to be a server side script or snipped which I need to write in server.js which should be routing the http to https, i tried to do it and it run into endless redirection. So questions -

  • Is there any guide to do this from AWS ?
  • If not then how one can achieve this, any pointers or suggestions would be greatly appreciated.
Jeet
  • 5,569
  • 8
  • 43
  • 75

4 Answers4

0

On the server side you can check the X-Forwarded-Proto (original request protocol) and if it's heaving value http you can send redirect (http 302) to a url with https protocol..

though with ALB (application load balancer you may specify a set of rules, maybe it's possible to do that there..)

gusto2
  • 11,210
  • 2
  • 17
  • 36
0

I couldn't find a guide from AWS, but I will keep searching and update the answer in the case I find it.

Usually, when you write applications in Node.js, you specify which port should your app run at. It means that you will need two different servers listening. And when your app receives a request on port 80 (HTTP), it should redirect to your HTTPS server, like in this answer.

Another point that may be relevant to your question is that, in production environments, you don't usually bind a port to your Node.js server, since it's not production ready. You probably want to use a reverse proxy and load balancer like Nginx or HAProxy.

Pedro Henrique
  • 332
  • 1
  • 2
  • 13
0

If you are using the AWS ALB (Application Load Balancer) they announced the http->https redirect today. Take a look: https://exampleloadbalancer.com/redirect_demo.html

CSA
  • 393
  • 4
  • 10
0

Put your ELB behind the Cloudfront and in settings of your distribution select forward HTTP to HTTPS. enter image description here The following doc will be helpful https://docs.aws.amazon.com/waf/latest/developerguide/tutorials-ddos-cross-service-ELB.html

This method has two benefit:

1-Your problem will be solve

2-You can use the benefit of the powerful CDN, for more information about Cloudfront read https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html

Update:

You can forward traffic from HTTP to HTTPS by edit your Listeners setting in your ELB. enter image description here

Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48