让我们encryption自动redirect到HTTPS不起作用

我已经在我的小Ubuntu服务器上设置了让我们encryption,并将其用于所有非IDN网站上。 它可以select自动将HTTP站点redirect到HTTPS。 我select了这个选项。

Let's Encrypt守护程序向每个域conf添加了三行,并为每个域创build了一个新的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, eg #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> 

我创build了这个文件,但Let's Encrypt守护进程在最后添加了重写规则。 它还创build了新文件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, eg #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请求redirect到HTTPS,因此您需要从*:443虚拟主机configuration文件中删除重写configuration。

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

 <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中启用了vhostconfiguration。 在Debian或Ubuntu中,您可以使用命令a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME"