将Elastic Beanstalk URL重定向到域名

时间:2019-07-20 02:43:40

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

我有一个托管在AWS Elastic beantalk上的应用程序,该应用程序被分配了以下环境URL:

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

我也已经注册了一个域名,

my-appname.com

在AWS Route 53中,我有一个A ALIAS指向my-appname.com这样的EB环境:

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

在我的注册商处,我设置了Route 53名称服务器来通过Amazon管理DNS。

一切正常

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

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

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

HTTP_HOST更改为my-appname.com会是一个好习惯吗?

编辑:该方法似乎仍然无法正常工作。不知道为什么吗?

2 个答案:

答案 0 :(得分: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(取自2020/09/08的Amazon Linux 2):

# 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证书管理器(ACM):为example.com和www.example.com
  • 创建一个证书
  • 路线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连接)

答案 1 :(得分:0)

我目前的理解是最好的方法,这是使用服务器级重写来解决此问题。一个示例(对于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]