【问题标题】:Redirecting from Http to Https with AWS's ELB使用 AWS 的 ELB 从 Http 重定向到 Https
【发布时间】:2017-09-30 05:16:02
【问题描述】:

我在here 之前进行过查询。我认为它正在工作,但实际上还没有。

我的负载均衡器面向 Internet,后面是 Ec2 实例。 我的负载均衡器对端口 80 和端口 443 开放。

它们排列为 80 -> 80 和 443 -> 80。

现在我需要将 http 访问重定向到 https。

我所做的是 我制作了一个 .htaccess 文件并位于 /var/www/html/ 中。那是我网站的 index.php 所在的文件夹。

在 .htaccess 文件中,我尝试了几个我在互联网上找到的版本。他们都没有工作。

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
    RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>

<VirtualHost *:80>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} www.(.+) [OR,NC]    # Added
    RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
    RewriteRule ^/?(.*) https://domainname.com%{REQUEST_URI} [L,R=301]
</VirtualHost>

为什么它在我的设置中不起作用?

编辑:

<If "req('X-Forwarded-Proto') != 'https'>
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
    RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</If>

【问题讨论】:

  • 我收到 AWS 的回复,提示 ELB 只是使用 X-Forwarded-Proto 插入重定向信息。所以重定向的实际实现需要在实例中发生,在 apache 中。我需要弄清楚如何。
  • Phabricator 解决了类似的问题。 stackoverflow.com/a/48149369/7098257

标签: .htaccess amazon-web-services amazon-ec2 url-redirection amazon-elb


【解决方案1】:

我使用 apache 为 HTTPS 所做的是通过 container commands 上传自定义的 redirect.conf 文件。所以redirect.conf 看起来像这样:

<If "req('X-Forwarded-Proto') != 'https'>
    RewriteEngine On
    RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</If>

然后你可以使用容器命令把这个文件放到 apache 配置目录/etc/httpd/conf.d/ 中,像这样:

container_commands:
  01_update_rewrite_rules:
    command: "cp rewrite.conf /etc/httpd/conf.d/"

【讨论】:

  • 你的意思是把它放在 /etc/httpd 文件夹里面,里面有 conf.d 文件。我找不到 /etc/httpd 文件夹。与 conf.d 相同
  • 您使用的是哪个 AMI? /etc/httpd/conf.d/是linux上默认的apache配置目录
  • 我使用的是 Ubuntu 16.06
  • 明白了,只是为了清楚这将在您的 ec2 实例上,而不是在您的个人计算机上(除非您也有 apache)。
  • 检查/etc/apache2/ 它应该有一个名为conf.d 的子目录。这就是您要放置自定义配置文件的位置
【解决方案2】:

我已经解决这个问题很长时间了,现在可以让它成功运行。我喜欢分享我所做的事情如下。

(1)Create .htaccess file inside the folder same as root file index.php exists
(2).htaccess file has
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
(3)Then enable mod_rewrite, try the command line command: 
   sudo a2enmod rewrite
(4)To make sure mod_rewrite is enabled, update /etc/apache2/apache2.conf file as
<Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>
initially is 
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Then you can setup http to https successfully
You need to restart server as sudo service apache2 restart after update of /etc/apache2/apache2.conf 

您的负载均衡器及其安全组需要开放 http 访问。

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 2023-03-22
    • 1970-01-01
    • 2018-04-15
    • 2018-03-07
    • 2019-01-23
    • 1970-01-01
    • 2018-08-18
    • 2017-12-25
    相关资源
    最近更新 更多