Redirecting EC2 Elastic Load Balancer from HTTP to HTTPS

amazon-ec2, amazon-elb, https, nginx, redirect

Solution

AWS Application Load Balancers now support native HTTP to HTTPS redirect.

To enable this in the console, do the the following:

- Go to your Load Balancer in EC2 and tab "Listeners"

- Select "View/edit rules" on your HTTP listener

- Delete all rules except for the default one (bottom)

- Edit default rule: choose "Redirect to" as an action, leave everything as default and enter "443" as a port.

The same can be achieved by using the CLI as described here.

It is also possible to do this in Cloudformation, where you need to set up a Listener object like this:

  HttpListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: 80
      Protocol: HTTP
      DefaultActions:
      - Type: redirect 
        RedirectConfig:
          Protocol: HTTPS
          StatusCode: HTTP_301
          Port: 443

If you still use Classic Load Balancers, go with one of the NGINX configs described by the others.

Problem

I want to redirect all the HTTP request to https request on ELB. I have two EC2 instances. I am using nginx for the server. I have tried a rewriting the nginx conf files without any success. I would love some advice on it.

Original source

Related problems