Apache2

Let’s Encrypt 自動重定向到 HTTPS 不起作用

  • May 29, 2017

我已經在我的小型 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 webmaster@timothy.green.name
   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 webmaster@timothy.green.name
   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 虛擬主機配置應如下所示:

&lt;VirtualHost *:80&gt;
 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]
&lt;/VirtualHost&gt;

確保您已經在 Apache 中啟用了虛擬主機配置。在 Debian 或 Ubuntu 中,您可以使用命令a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME".

引用自:https://serverfault.com/questions/816392