【问题标题】:Adding www to domain in elasticbeanstalk ngnix在弹性beantalk nginx中将www添加到域
【发布时间】:2016-04-09 19:02:04
【问题描述】:

我有一个在 ElasticBeanstalk 上运行的 NodeJS 应用程序。我想将所有到 basedomain.com 的请求重定向到 www.basedomain.com(不包括子域)并添加 https。

我已更新 .ebextension 文件夹中的 .config 文件以使用以下配置文件重定向到 https:

files:
  "/tmp/45_nginx_https_rw.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash

      CONFIGURED=`grep -c "return 301 https" /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf`

      if [ $CONFIGURED = 0 ]
        then
          sed -i '/listen 8080;/a \    if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
          logger -t nginx_rw "https rewrite rules added"
          exit 0
        else
          logger -t nginx_rw "https rewrite rules already set"
          exit 0
      fi

container_commands:
  00_appdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact
  01_configdeploy_rewrite_hook:
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact
  02_rewrite_hook_perms:
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh
  03_rewrite_hook_ownership:
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh

有人可以帮忙添加 www 吗?

【问题讨论】:

    标签: node.js nginx amazon-elastic-beanstalk


    【解决方案1】:

    最好使用 DNS 重定向而不是 nginx。每个 DNS 提供商都有一个裸域重定向解决方案,您可以使用该解决方案将流量从裸域转移到 www。
    例如,在 Route 53 中,您使用别名记录执行此操作。为此,请打开 basedomain.com 的 Route 53 托管区域并创建一个新记录集。将名称留空。选择A`类型。将别名设置为 Yes 并从 Alias Target 下拉列表的 S3 Website Endpoints 部分中选择 basedomain.com。您的域 basedomain.com 现在重定向到 www.basedomain.com

    【讨论】:

    • Route53 不支持 DNS 重定向,因此无法采用该解决方案
    • A 记录可以完成您需要的工作。我已经更新了答案。
    【解决方案2】:

    您将需要一个单独的服务器,其工作是进行重定向。将 www.foo.com 路由到 foo.com 的示例 nginx 配置将是:

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

    然后在 DNS 中,您将 A 或 CNAME www.foo.com 指向从上面运行 nginx 的服务器,并将 foo.com 的 A 或 CNAME 记录指向 beanstalk 服务器。

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 2018-01-12
      • 2018-03-16
      • 2015-10-10
      • 1970-01-01
      • 2014-01-03
      • 2015-05-22
      • 2015-10-14
      • 2020-12-31
      相关资源
      最近更新 更多