我已经在我的小型 Ubuntu 服务器上设置了 Let's Encrypt,并在其上的所有非 IDN 站点上使用它。它可以选择自动将 HTTP 站点重定向到 HTTPS。我选择了那个选项。
Let's Encrypt 守护进程在每个域 conf 中添加了三行,并为每个域创建了一个新的 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 守护进程做到了。所以我假设语法是正确的。
您只需将请求从 HTTP 重定向到 HTTPS,因此您需要从 *:443 虚拟主机配置文件中删除重写配置。
端口 80 虚拟主机配置应如下所示:
确保您已经在 Apache 中启用了虚拟主机配置。在 Debian 或 Ubuntu 中,您可以使用命令
a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME"
.