• Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

113
Views
How to redirect http -> https on aws classic load balancer?

I have a classic load balancer on beanstalk and configured nginx instances. I want to redirect http to https requests.

I setup my load balancer listeners to redirect to port 80 to its instances.

I created a file in .ebextensions/nginx_config.config, in which I setup the redirect and also filter out the healthcheck.

See the config rewrite below:

files:
   /etc/nginx/conf.d/proxy.conf:
     owner: root
     group: root
     mode: "000644"
     content: |

       upstream nodejs {
           server 127.0.0.1:8081;
           keepalive 256;
       }

       server {
           listen 80;


           if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
               set $year $1;
               set $month $2;
               set $day $3;
               set $hour $4;
           }
           access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
           access_log  /var/log/nginx/access.log  main;


           location / {
               set $redirect 0;
               if ($http_x_forwarded_proto = "http") {
                return 301 https://$host$request_uri;
                }
               if ($http_user_agent ~* "ELB-HealthChecker") {
                 set $redirect 0;
               }
               if ($redirect = 1) {
                 return 301 https://$host$request_uri;
               }

               proxy_pass  http://nodejs;
               proxy_set_header   Connection "";
               proxy_http_version 1.1;
               proxy_set_header        Host            $host;
               proxy_set_header        X-Real-IP       $remote_addr;
               proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
           }

           location /health-check {
            access_log off;
            default_type text/plain;
            return 200 ‘OK’;
           }

       gzip on;
       gzip_comp_level 4;
       gzip_types text/html text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

       }

   /opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
     owner: root
     group: root
     mode: "000755"
     content: |
       #!/bin/bash -xe
       rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
       if [[ -e /etc/init/nginx.conf ]] ; then
         echo Using initctl to stop and start nginx
         initctl stop nginx || true
         initctl start nginx
       else
         echo Using service to stop and start nginx
         service nginx stop 
         service nginx start
       fi

container_commands:
  removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

But nothing seems to happen and the server still doesn't redirect to https. It seems like my config is just being ignored. How can I redirect to https in this case?

11 months ago · Santiago Trujillo
2 answers
Answer question

0

So as per my suggestion above. Create an Application Load Balancer with 2 listeners.

1st Listener is a 443 HTTPS listener that serves traffic directly to your target group.

2nd Listener is a 80 HTTP listener that uses a redirect rule that does a redirect to HTTPS.

This is best practice.

11 months ago · Santiago Trujillo Report

0

You can have your load balancer listen on 443 with a certificate from ACM and then redirect that traffic to port 80? But it's highly suggested to used a ALB as said by @mokugo-devops above. Hope this helps. You can also look at a similar issue AWS EB - Redirect all traffic to https

11 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.