如何在AWS Elastic Beanstalk上强制HTTPS

时间:2018-01-09 01:25:03

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

使用具有Nginx负载均衡器的Elastic Beanstalk强制http到https的最佳方法是什么? Https使用我从AWS Certificate Manager收到的证书为应用程序工作,我只是想让它确保始终使用https。似乎应该已经内置到AWS中的东西。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

在ebextensions中创建一个以.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 8080;


           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 != "https") {
                 set $redirect 1;
               }
               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;
           }

       gzip on;
       gzip_comp_level 4;
       gzip_types text/html text/plain text/css application/json 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
       service nginx stop 
       service nginx start

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"

source

您可以阅读有关ebextensions here

的更多信息