如何将 elasticbeanstalk.com 上的 Apache 支持的代理重定向到自定义域?

Posted

技术标签:

【中文标题】如何将 elasticbeanstalk.com 上的 Apache 支持的代理重定向到自定义域?【英文标题】:How to redirect an Apache backed proxy on elasticbeanstalk.com to a custom domain? 【发布时间】:2014-04-11 08:01:20 【问题描述】:

我正在使用 Tomcat 在 Elastic Beanstalk 中运行应用程序。我为我的环境设置了自己的域名,并将其添加为 xyz.elasticbeanstalk.com 地址的 CNAME 别名,并且一切正常。我想做的是将访问xyz.elasticbeanstalk.com 站点的流量重定向到我自己的域名。

我想我可以像这样在我的 WAR 文件中使用 .ebextensions 来做到这一点:

myapp.config:

container_commands:
    01_setup_rewrite:
        command: "cp .ebextensions/redirect_canonical.conf /etc/httpd/conf.d/redirect_canonical.conf"
    02_apache_restart:
        command: /etc/init.d/httpd restart

redirect_canonical.conf:

RewriteEngine On
RewriteCond %HTTP_HOST   !^www\.mydomain\.com [NC]
RewriteCond %HTTP_HOST   !^$
RewriteRule ^/?(.*)         http://www.mydomain.com/$1 [L,R,NE]

我之前在 .htaccess 文件中使用过类似的 mod_rewrite 规则,它们运行良好,但因为这是一个 mod_proxy,我不确定这是否正确(或者我什至可以将 rewrite 命令放入 conf. d 文件夹)。

我如何配置它做我想要的重定向?

【问题讨论】:

你最后搞清楚了吗? 【参考方案1】:

来自 Mason G. Zhwiti 对相关问题 How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS? 的回答,我认为您需要以类似的方式处理 Apache,例如类似这样的东西(由于没有时间实际测试它而省略了 Bash 脚本):

files:
  "/tmp/45_apache_redirect_canonical.sh":
    owner: root
    group: root
    mode: "000644"
    content: |
      #! /bin/bash
      # TODO: create script that injects content of your 'redirect_canonical.conf' 
      #       into (presumably) '/etc/httpd/conf.d/00_elastic_beanstalk_proxy.conf'

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

祝你好运!

【讨论】:

以上是关于如何将 elasticbeanstalk.com 上的 Apache 支持的代理重定向到自定义域?的主要内容,如果未能解决你的问题,请参考以下文章

如何将根域指向 Elastic Beanstalk?

AWS 和 Elastic Beanstalk

将弹性豆茎(wordpress 博客)映射到子域

将 AWS Elastic Beanstalk URL 重定向到我自己的域

AWS Elastic Beanstalk - 将负载均衡器追溯添加到应用程序

AWS Route 53 别名记录导致 301