Certbot 无法为 Apache2 网站实现重定向到 https

Certbot 无法为 Apache2 网站实现重定向到 https

问题
我想了解如何使用带有 Certbot 认证的 Apache2 实现从 http 到 https 的重定向。

细节
在运行 Ubuntu 16.04 的新服务器上,我安装了一个“完美服务器”设置(ISPConfig、Postfix、Dovecot 及其家族)来运行小型电子邮件服务器。

我还使用 Certbot 安装 SSL 证书。在使用 Certbot 之前,我手动添加了在目录ServerName my.domain.com中找到的四个文件/etc/apache2/sites-available/...

  • 00-默认.conf
  • 应用程序.vhost
  • 默认 SSL 配置文件
  • ispconfig.vhost

...还有这个文件:

  • /etc/apache2/apache2.conf

我的理解是,这为 Ce​​rtbot 提供了有关服务器上存在哪些域名的信息,并确保能够满足挑战。(也许这有点过头了?也许只编辑 `apache2.conf 就足够了?)

然后我跑了:

certbot --apache -d my.domain.com

在安装过程中,我选择了重定向选项:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

这是 Certbot 做出的承诺:

Redirecting vhost in /etc/apache2/sites-enabled/000-apps.vhost to
ssl vhost in /etc/apache2/sites-enabled/000-ispconfig.vhost

然而,有两个问题:

  1. 未针对端口 :80 上的默认站点提出重定向
  2. Certbot 所讨论的文件从未存在过,正如您在此处所见:

# ls -al /etc/apache2/sites-available/
total 36
drwxr-xr-x 2 root root 4096 Nov  9 17:29 .
drwxr-xr-x 9 root root 4096 Nov  9 16:44 ..
-rw-r--r-- 1 root root 1336 Nov  9 14:35 000-default.conf
-rw-r--r-- 1 root root 1340 Nov  9 16:44 apps.vhost
-rw-r--r-- 1 root root 1200 Nov  9 16:33 apps.vhost.save
-rw-r--r-- 1 root root 6387 Nov  9 15:08 default-ssl.conf
-rw-r--r-- 1 root root 1929 Nov  9 12:36 ispconfig.conf
-rw-r--r-- 1 root root 3349 Nov  9 16:44 ispconfig.vhost

现在,当我连接到http://my.domain.com,我看到了 Apache2 Ubuntu 默认页面,没有重定向。如果我连接到https://my.domain.com,则所有浏览器都会告诉我有问题。Firefox 最明确:

Secure Connection Failed

An error occurred during a connection to my.domain.com.
SSL received a record that exceeded the maximum permissible length.
Error code: SSL_ERROR_RX_RECORD_TOO_LONG

The page you are trying to view cannot be shown because the
authenticity of the received data could not be verified.

然而,当我访问 ISPConfig 页面时https://http://my.domain.com:8080/,一切都很顺利。浏览器很高兴在地址栏右侧显示绿色的“安全”标志。

参观http://http://my.domain.com:8080/很有礼貌,但不太成功:

Bad Request

Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

我注意到 Certbot 对 处的文件进行了更改/etc/apache/sites-available/ispconfig.vhost。在下面的摘录中,标记的行已被删除,并且已添加---行。+++

# SSL Configuration
  SSLEngine On
  SSLProtocol All -SSLv3
  --- SSLCertificateFile /usr/local/ispconfig/interface/ssl/ispserver.crt
  --- SSLCertificateKeyFile /usr/local/ispconfig/interface/ssl/ispserver.key
  #SSLCACertificateFile /usr/local/ispconfig/interface/ssl/ispserver.bundle

  SSLCipherSuite ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-S$
  SSLHonorCipherOrder On

  <IfModule mod_headers.c>
  Header always add Strict-Transport-Security "max-age=15768000"
  RequestHeader unset Proxy early
  </IfModule>

  SSLUseStapling On
  SSLStaplingResponderTimeout 5
  SSLStaplingReturnResponderErrors Off
  +++ SSLCertificateFile /etc/letsencrypt/live/my.domain.com/fullchain.pem
  +++ SSLCertificateKeyFile /etc/letsencrypt/live/my.domain.com/privkey.pem
  +++ Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

(Certbot 还添加了一个/etc/letsencrypt/包含所有认证优点的目录。)

我的目标
我想了解我需要对文件进行哪些更改,/etc/apache2/sites-available以便http://my.domain.com被悄悄重定向到https://my.domain.com,既适用于主站点,也适用于端口 :8080 上的 ISPConfig 页面。

换句话说,我想知道如何对真实文件执行 Certbot 所说的对仅存在于其想象中的文件执行的操作。

答案1

这里的主要问题是配置中的服务器条目重复。因此,Apache 可能只加载了部分服务器配置选项。

certbot --apache(不带选项运行certonly)读取 Web 服务器配置,并尝试自动更新已获得证书的站点的配置。如果 Web 服务器配置中有多个站点条目,它可能会更新未被读取的配置 - 我认为这就是这里发生的情况。

有几种不同的方法来组织 Web 服务器配置文件,很容易混淆。你打算使用哪一种?这个服务器故障问题可能有帮助。

编辑:重新阅读有关 certbot 重定向的问题 - 删除 certbot 添加的行并certbot apache -d example.com在组织配置后再次运行应该允许 certbot 自动添加重定向选项。

相关内容