1

我有一个通过弹性负载均衡器 (ELB) 在​​ AWS 上设置的域。为了启用 SSL,我已将 AWS 托管安全证书 (*.domain.us) 添加到负载均衡器。我需要能够安全地访问以下 8 个 URL 组合:

[1] http://www.domain.us
[2] http://www.subdomain.domain.us
[3] http://domain.us
[4] http://subdomain.domain.us
[5] https://www.domain.us
[6] https://www.subdomain.domain.us
[7] https://domain.us
[8] https://subdomain.domain.us

凭借通配符 AWS 证书,1、4、5 和 8 自动为我工作。但是对于剩余的 URL 类型,我已经更新了我的 .htaccess 文件如下:(如果我只是访问 [7] 而不将其转换为 www.domain.us,我会收到“连接不安全”错误,因为我的 AWS 证书是通配符一个并且不能简单地在 https://.us 上工作。但是 https:// www .domain.us 或 https://域 .domain.us 工作正常,因为通配符证书)

RewriteEngine On

# domain.us => www.domain.us -------------------------------------------
RewriteCond %{HTTP_HOST} ^domain.us
RewriteRule ^ http://www.domain.us%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^domain.us
RewriteRule ^ https://www.domain.us%{REQUEST_URI} [R=301,L]

# www.subdomain.domain.us => subdomain.domain.us -----------------------
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.domain\.us)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

# NO WWW   http://www. becomes always http://
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.+\.domain\.us)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# http => https ------------------------------------------------------------
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

<Files 403.shtml>
order allow,deny
allow from all
</Files>

我在 ELB 上监听 80 和 443 端口,并将每个端口的对应数据发送到实例端口。我在 EC2 实例上打开了 80 和 443 端口。

我已经浏览了许多 SO 帖子并在 .htaccess 文件上方创建了,但我仍然无法拒绝。[6] 和 [7] 有效。我仍然在浏览器中收到“连接不安全”错误。有人可以帮忙吗?

4

1 回答 1

0

这是一个非常常见的“问题”,或者更确切地说是按设计工作。您在浏览器中显式调用 HTTPS,因此在交换任何数据之前建立 HTTPS 连接。但是当你的证书不涵盖时它注定会失败

[6] https://www.subdomain.domain.us
[7] https://domain.us

您必须在处理 SSL-/TLS-Offloading 的组件中实现逻辑,以便在建立 SSL 连接之前首先更改为 http 或正确的 HTTP_HOST。

所以你的方法是:

不要使用显式 HTTPS 调用 URL!!!:)

让服务器执行此操作。您的服务器配置可以缩短为:

RewriteCond %{HTTP_HOST} !^.+\.domain.us
RewriteRule ^ https://www.domain.us%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.+\.domain\.us)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,QSA,L]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

请注意,%{REQUEST_URI} 不包括%{QUERY_STRING}。所以查询字符串总是在你的重写中被丢弃。

于 2017-04-03T15:34:08.077 回答