AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 816392
Accepted
TRiG
TRiG
Asked: 2016-11-23 09:53:20 +0800 CST2016-11-23 09:53:20 +0800 CST 2016-11-23 09:53:20 +0800 CST

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

  • 772

我已经在我的小型 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 守护进程做到了。所以我假设语法是正确的。

apache2 lets-encrypt
  • 1 1 个回答
  • 11854 Views

1 个回答

  • Voted
  1. Best Answer
    Zuko
    2016-11-23T10:24:41+08:002016-11-23T10:24:41+08:00

    您只需将请求从 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 中启用了虚拟主机配置。在 Debian 或 Ubuntu 中,您可以使用命令a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME".

    • 1

相关问题

  • HTTPS 子域不重定向非通配符证书

  • 为什么 `apache2 -k graceful` 会出现此错误?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve