【问题标题】:How to redirect http -> https on aws classic load balancer?如何在 aws 经典负载均衡器上重定向 http -> https?
【发布时间】:2020-08-23 10:12:22
【问题描述】:

我在 beanstalk 上有一个经典的负载均衡器并配置了 nginx 实例。我想将 http 重定向到 https 请求。

我将负载均衡器侦听器设置为将端口 80 重定向到其实例。

我在 .ebextensions/nginx_config.config 中创建了一个文件,我在其中设置了重定向并过滤掉了运行状况检查。

请参阅下面的配置重写:

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"

但似乎什么都没有发生,服务器仍然没有重定向到 https。似乎我的配置被忽略了。在这种情况下如何重定向到 https?

【问题讨论】:

  • 有什么理由不能使用 ALB?

标签: amazon-web-services nginx https amazon-elastic-beanstalk amazon-elb


【解决方案1】:

所以按照我上面的建议。创建一个具有 2 个侦听器的 Application Load Balancer。

第一个侦听器是一个 443 HTTPS 侦听器,可直接为您的目标组提供流量。

第二个侦听器是一个 80 HTTP 侦听器,它使用 redirect rule 重定向到 HTTPS。

这是最佳做法。

【讨论】:

    【解决方案2】:

    您可以让负载均衡器使用 ACM 的证书在 443 上侦听,然后将该流量重定向到端口 80?但强烈建议使用上面@mokugo-devops 所说的 ALB。希望这可以帮助。也可以看看类似的问题AWS EB - Redirect all traffic to https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-18
      • 2017-02-19
      • 1970-01-01
      • 2020-08-21
      • 2023-04-01
      • 2018-06-08
      • 2017-03-10
      • 2021-07-21
      相关资源
      最近更新 更多