Let's Encrypt 自动重定向到 HTTPS 不起作用

Let's Encrypt 自动重定向到 HTTPS 不起作用

我已经在我的小型 Ubuntu 服务器上设置了 Let's Encrypt,并在其上的所有非 IDN 网站上使用它。它有自动将 HTTP 站点重定向到 HTTPS 的选项。我选择了该选项。

Let's Encrypt 守护程序向每个域配置添加了三行,并为每个域创建了一个新的 domain-le-ssl.conf。

这里是timothy.green.name.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName timothy.green.name
    ServerAdmin [email protected]
    DocumentRoot /var/www/vhosts/timothy.green.name/web

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =timothy.green.name
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

我创建了这个文件,但 Let's Encrypt 守护进程在最后添加了重写规则。它还创建了新文件timothy.green.name-le-ssl.conf,内容如下:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName timothy.green.name
    ServerAdmin [email protected]
    DocumentRoot /var/www/vhosts/timothy.green.name/web

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =timothy.green.name
# Some rewrite rules in this file were were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/myh2g2.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/myh2g2.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
</IfModule>

这一切看起来都正确。并且mod_rewrite已到位:

$ a2enmod rewrite
Module rewrite already enabled

然而,尽管https://timothy.green.name工作正常,http://timothy.green.name给我一个 Apache 默认站点。这里可能出了什么问题?我重申一下,这些重写规则不是我自己添加的:是 Let's Encrypt 守护进程添加的。所以我假设语法是正确的。

答案1

您只需将请求从 HTTP 重定向到 HTTPS,因此您需要从 *:443 虚拟主机配置文件中删除重写配置。

端口 80 虚拟主机配置应如下所示:

<VirtualHost *:80>
  RewriteEngine on
  RewriteCond %{SERVER_NAME} =domain1.com [OR]
  RewriteCond %{SERVER_NAME} =domain2.com [OR]
  RewriteCond %{SERVER_NAME} =domain3.xxx [OR]
  RewriteCond %{SERVER_NAME} =maindomain.yyy
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
</VirtualHost>

确保您已在 Apache 中启用 vhost 配置。在 Debian 或 Ubuntu 中,您可以使用命令a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME"

相关内容