如何在Elasticbeanstalk(nginx服务器)中将www重定向到非www和http重定向到https

如何在Elasticbeanstalk(nginx服务器)中将www重定向到非www和http重定向到https,nginx,amazon-elastic-beanstalk,Nginx,Amazon Elastic Beanstalk,我使用Elasticbeanstalk(nginx服务器)和负载均衡器以及路由53 成功:能够使用以下代码/配置将http://重定向到https:// 失败:重定向http://www.和https://www.至https://(不含www) 我注意到www根本不工作(无法连接到服务器) 我做错了什么?我是不是错过了53号公路 或者如何重定向http://www.domian,https://www.domian和http://domian至https://domian 更新:我设法使用此解

我使用Elasticbeanstalk(nginx服务器)和负载均衡器以及路由53

成功:能够使用以下代码/配置将
http://
重定向到
https://

失败:重定向
http://www.
https://www.
https://
(不含www)

我注意到www根本不工作(无法连接到服务器)

我做错了什么?我是不是错过了53号公路

或者如何重定向
http://www.domian
https://www.domian
http://domian
https://domian

更新:我设法使用此解决方案使www.work正常工作

但仍然不知道如何做所有的重定向

代码/配置:

###################################################################################################
#### Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
####
#### Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
#### except in compliance with the License. A copy of the License is located at
####
####     http://aws.amazon.com/apache2.0/
####
#### or in the "license" file accompanying this file. This file is distributed on an "AS IS"
#### BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#### License for the specific language governing permissions and limitations under the License.
###################################################################################################

###################################################################################################
#### This configuration file configures Nginx for Node.js environments to redirect HTTP
#### requests on port 80 to HTTPS on port 443 after you have configured your environment to support
#### HTTPS connections:
####
#### Configuring Your Elastic Beanstalk Environment's Load Balancer to Terminate HTTPS:
####  http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-elb.html
####
#### Terminating HTTPS on EC2 Instances Running Node.js:
####  http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/https-singleinstance-nodejs.html
###################################################################################################

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

       # Elastic Beanstalk managed configuration file
       # Some configuration of nginx can be by placing files in /etc/nginx/conf.d
       # using Configuration Files.
       # http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html 


       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
       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"
这非常有效:

  • 将路由53中的www和非www A指针添加到EBS。确保您的SSL证书同时适用于www和非www。使用向导模式可以更轻松地选择您的EBS环境

  • 将下面的条目添加到
    nginx.conf
    文件中

    server {
      listen 80;
      server_name www.example.com;
    
      return 301 https://example.com$request_uri;
    }