Apache HTTP 到 HTTPS 重定向适用于别名,但不适用于服务器名称

Apache HTTP 到 HTTPS 重定向适用于别名,但不适用于服务器名称

我使用 Let's Encrypt 创建了一个 SSL 证书。
该证书已安装并在 Apache 配置文件中引用。Apache
配置提供了 ServerName(例如 example.com)和 ServerAlias(例如 www.example.com)。这两者都在 SSL 证书中引用,这已由命令的输出验证certbot certificates

Apache 配置还包含从 HTTP 到 HTTPS 的重定向。
RedirectMatch permanent ^/(.*) https://example.com/$1

问题是重定向仅针对别名 (www.example.com) 发生。对 example.com 的请求将导致简单的 200 OK 响应。

请求别名:

$ curl -I www.example.com
HTTP/1.1 301 Moved Permanently
Date: Sun, 01 Mar 2020 19:13:57 GMT
Server: Apache/2.4.29 (Ubuntu)
Location: https://example.com/
Content-Length: 325
Content-Type: text/html; charset=iso-8859-1

请求实际的ServerName值:

curl -I example.com
HTTP/1.1 200 OK
Date: Sun, 01 Mar 2020 19:16:43 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Sun, 23 Feb 2020 00:03:31 GMT
ETag: "2aa6-59f32fc296ba5"
Accept-Ranges: bytes
Content-Length: 10918
Vary: Accept-Encoding
Content-Type: text/html

配置文件的和部分除 *:80 部分中的重定向和 *:443 部分中的 SSL 行外,其他部分相同<VirtualHost *:80><VirtualHost *:443>

SSLCertificateFile /etc/letsencrypt/live/example/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

我是否忽略了一些显而易见的东西,或者我已经走得太远了?:-) 谢谢

答案1

明白了!:-) 在这里发布答案以防其他可怜的人犯和我一样的错误。

事实证明,有一个具有相同服务器名称的<VirtualHost *:80>条目( )。该文件不包含 SSL 配置指令。000-default.confServerName example.com000-default.conf

看起来,如果相同的内容ServerName出现在多个启用的 Apache 配置文件中,哪个条目优先是不确定的。

我只需将ServerName指令更改为000-default.conf包含主机名并重新启动 Apache。问题解决了。

相关内容