【问题标题】:Redirect Elastic Beanstalk URL to domain name将 Elastic Beanstalk URL 重定向到域名
【发布时间】:2019-11-28 23:27:20
【问题描述】:

我有一个托管在 AWS Elastic Beanstalk 上的应用程序,它被分配了一个环境 URL,如下所示:

<my-appname>.<aws-region>.elasticbeanstalk.com

我也注册了这样的域名:

my-appname.com

在 AWS Route 53 中,我有一个 A ALIASmy-appname.com 指向 EB 环境,如下所示:

my-appname.com > A ALIAS <my-appname>.<aws-region>.elasticbeanstalk.com

从我的注册商那里,我设置了 Route 53 域名服务器以通过 Amazon 管理 DNS。

一切正常

我想了解如何做的是确保对<my-appname>.<aws-region>.elasticbeanstalk.com> 域的任何请求都将301 发送到my-appname.com 域。

我目前正在使用 Apache RewriteRule 将所有非 www 请求重定向到网站的 www 版本,并在 .config 文件中使用它:

<If "'%{HTTP_HOST}' !~ /^www\./">
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</If>

HTTP_HOST 更改为my-appname.com 是否是一种好习惯?

编辑:无论如何,这种方法似乎不起作用。不知道为什么?

【问题讨论】:

  • &lt;my-appname&gt;.&lt;aws-region&gt;.elasticbeanstalk.com&gt; 只是 Amazon 创建的一个友好 URL,它使您可以更轻松地通过名称而不是 Env 来测试和识别应用程序。 ID。作为一般规则,它们不应公开,因为它们是您作为 AWS 客户没有权限或控制权的子域。我不认为环境。创建应用程序后,可以更改 URL。但是,您可以克隆它,创建一个您保持私有的新 URL,更新 Route53 中的必要配置,然后终止您的原始应用程序 - 永远终止该 URL。
  • 我担心的是,如果 URL 公开(由于意外或不知情的意图),它将代表 SEO 头痛。我认为服务器重写规则应该足够了,例如Rewrite Cond %{HTTP_HOST} !~ my-appname.tld

标签: apache amazon-web-services dns amazon-elastic-beanstalk amazon-route53


【解决方案1】:

使用 Elastic Beanstalk (Amazon Linux 2) 和 Nginx 时,您有两种解决方案:

扩展 Elastic Beanstalk 默认 nginx.conf

在您的源代码中创建一个名为 .platform/nginx/conf.d/redirections.conf 的文件,其中包含:

server {
    server_name .elasticbeanstalk.com;
    return 301 https://example.com$request_uri;
}

Nginx 文档:https://www.nginx.com/blog/creating-nginx-rewrite-rules/

(example.com 是您自己的域)

创建您自己的 nginx.conf 替换 Elastic Beanstalk 中的默认配置

  • 通过使用 SSH (*) 连接到您的 Elastic Beanstalk EC2 实例,从原始 /etc/nginx/nginx.conf 复制内容
  • 在您的源代码中创建一个名为 .platform/nginx/nginx.conf 的文件并粘贴内容
  • 根据您的需要修改并添加:
server {
    server_name .elasticbeanstalk.com;
    return 301 https://example.com$request_uri;
}

您最终应该得到如下所示的 /etc/nginx/nginx.conf(取自 Amazon Linux 2,截至 2020 年 9 月 8 日):

# Elastic Beanstalk Nginx Configuration File

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    32136;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    include       conf.d/*.conf;

    map $http_upgrade $connection_upgrade {
        default     "upgrade";
    }

    server {
        listen        80 default_server;
        access_log    /var/log/nginx/access.log main;

        client_header_timeout 60;
        client_body_timeout   60;
        keepalive_timeout     60;
        gzip                  off;
        gzip_comp_level       4;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        # Include the Elastic Beanstalk generated locations
        include conf.d/elasticbeanstalk/*.conf;
    }

    # ADDED
    server {
        server_name .elasticbeanstalk.com;
        return 301 https://example.com$request_uri;
    }
}


有关 Nginx 配置的更多信息

同时,我还建议对您的 Nginx 配置进行其他修改。

将 www 重定向到根目录

www.example.com 重定向到example.com 的示例。

# .platform/nginx/conf.d/redirections.conf

# https://stackoverflow.com/a/43089681
# https://tribulant.com/docs/hosting-domains/hosting/9867/redirecting-to-www-or-non-www/
# This can be done at the load balancer level but I prefer to do it here
# Test this with `curl --head https://www.example.com` and `curl --head http://www.example.com`
server {
    server_name www.example.com;
    return 301 https://example.com$request_uri;
}

先决条件:

  • AWS Certificate Manager (ACM):为 example.com 和 www.example.com 创建一个证书
  • Route 53:为 example.com 和 www.example.com 创建 A 记录,该路由到负载均衡器

HTTP 安全标头

为了安全起见,我建议设置这些 HTTP 标头:

# .platform/nginx/conf.d/security_headers.conf

# Remove Nginx version in error page and header
server_tokens off;

# Security headers thanks to https://observatory.mozilla.org/ and https://webpagetest.org/
# Inspired by https://www.mozilla.org/ HTTP headers
# https://gist.github.com/plentz/6737338
# https://github.com/GetPageSpeed/ngx_security_headers
add_header Content-Security-Policy "default-src 'self';
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options DENY;
add_header X-XSS-Protection "1; mode=block";

文件压缩(.js、.css、.html...)

您可以使用gzip on; 启用压缩。不幸的是you cannot extend the default nginx.conf to enable compression。您必须复制粘贴并修改原始 nginx.conf (.platform/nginx/nginx.conf)。

注意:您可以拥有自己的.platform/nginx/nginx.conf 并仍然使用.platform/nginx/conf.d/ 目录中的文件。

将 HTTP 重定向到 HTTPS

2 solutions:使用负载均衡器(Application Load Balancer)或自定义.platform/nginx/nginx.conf

# .platform/nginx/nginx.conf

...

    server {
        listen        80 default_server;

        ...

        # ADDED
        # [AWS documentation - Configuring HTTP to HTTPS redirection](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-httpredirect.html)
        # https://github.com/awsdocs/elastic-beanstalk-samples/blob/9720e38e9da155752dce132a31d8e13a27364b83/configuration-files/aws-provided/security-configuration/https-redirect/nodejs/https-redirect-nodejs.config#L61
        # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
        if ($http_x_forwarded_proto = "http") {
            return 301 https://example.com$request_uri;
        }

        ...
    }

...




(*) 在您的 EC2 实例安全组中打开端口 22(类似于 *AWSEBSecurityGroup*)然后转到:

EC2 > 实例 > 连接 > EC2 实例连接(基于浏览器的 SSH 连接)

【讨论】:

    【解决方案2】:

    我目前的理解是,最好的方法是使用服务器级别的重写来解决这个问题。一个示例(对于 Apache 服务器)如​​下:

    Rewrite Engine On
    
    # Catch requests to domains other than your primary (custom) domain
    Rewrite Cond %{HTTP_HOST} !~ appname.tld
    
    # Send those requests to the primary domain
    RewriteRule (.*) http://www.appname.tld%{REQUEST_URI} [R=301, L]
    

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2019-02-25
      • 2016-06-19
      • 2013-03-13
      • 2019-09-21
      • 2017-12-01
      • 2021-11-06
      • 2016-08-31
      • 2020-05-05
      相关资源
      最近更新 更多