codernoob8
codernoob8

Reputation: 712

Redirect http to https AWS Application Load Balancer

Hey everyone so I configured my ELB to use an SSL certificate and it works great, however, I still have a problem where if a user comes to my website on port 80 under HTTP the website does not redirect them to an HTTPS secure connection. Heres a screenshot of my ELB configuration as seen in the Elastic Beanstalk configuration tab. Any help is appreciated thank you. elb config

Upvotes: 1

Views: 2175

Answers (4)

Raphaël Lefevre
Raphaël Lefevre

Reputation: 19

In fact, you can no longer do it from the Elastic Beanstalk interface.

Also don't do it from the app, bad solution for load.

You must now go to the EC2 load balancer and change the listener to port 80, as described here:

https://medium.com/@j_cunanan05/how-to-redirect-http-to-https-in-amazon-web-services-aws-elastic-beanstalk-67f309734e81

Upvotes: 0

Vlad
Vlad

Reputation: 425

You can set up Application Load Balancer listener in the following way:

HTTP 80: default action 
IF 
Requests otherwise not routed
THEN 
Redirect to HTTPS://#{host}:443/#{path}?#{query}
Status code:HTTP_301

Upvotes: 0

stefansundin
stefansundin

Reputation: 3044

While it seems like you got it working by redirecting in your application, it is possible to do this redirect entirely in your ALB. Documentation: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html

This feature was released in July 2018. https://aws.amazon.com/about-aws/whats-new/2018/07/elastic-load-balancing-announces-support-for-redirects-and-fixed-responses-for-application-load-balancer/

Elastic Beanstalk may not have launched their own support to configure this. You may be able to configure it via ebextensions, but for now it may be best to just do it in your application.

Upvotes: 1

codernoob8
codernoob8

Reputation: 712

It wasn't the elb at all I simply had to add this code:

if (process.env.NODE_ENV === "production" || process.env.NODE_ENV === "awsDevelopmentServer") {
  app.use(function(req, res, next) {
    // Insecure request?
    if (req.get("x-forwarded-proto") == "http") {
      // Redirect to https://
      return res.redirect("https://" + req.get("host") + req.url);
    }```

Upvotes: 1

Related Questions