![虚拟主机 SSL 的正确配置](https://linux22.com/image/797798/%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA%20SSL%20%E7%9A%84%E6%AD%A3%E7%A1%AE%E9%85%8D%E7%BD%AE.png)
我已经在我的 Ubuntu EC2 实例上安装了 SSL 证书,并且我需要通过 https 访问此实例上托管的其中一个网站。
我通过虚拟主机将多个网站托管在同一个 IP 上。但是,我只需要一个网站可通过 https 访问。
我确信以下几点:
- SSL 证书已正确安装
- EC2 上的 443 端口已开放
我确信这些,因为当我在 /etc/apache2/sites-enabled/mysslsite 中尝试以下虚拟主机配置时,我可以通过 https 访问该站点。问题是所有其他网站都瘫痪了,因为它们也需要仅通过 https 访问。以下是虚拟主机配置文件:
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /var/www/mysslwebsite
ServerName www.mysslwebsite.com
ServerAlias mysslwebsite.com
<Directory /var/www/mysslwebsite>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
SSLEngine on
SSLCertificateFile /etc/ssl/apache2/mycertificate.com.crt
SSLCertificateKeyFile /etc/ssl/apache2/mycertificate.key
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost>
使用此配置,尽管它位于此特定的 mysslwebsite 虚拟主机配置中,但所有其他网站都不会通过标准 http 加载,并在通过 http 访问时显示以下消息:
Bad Request
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.
Hint: https://www.myothersite.com/
有人知道我该如何解决这个问题吗?
谢谢
- - - -编辑 - - - -
我尝试了以下虚拟主机:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/mysslsite
ServerName www.mysslsiste.com
ServerAlias mysslsite.com
<Directory /var/www/mysslsite>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot /var/www/mysslsist
ServerName www.mysslsist.com
ServerAlias mysslsist.com
<Directory /var/www/mysslsist>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
SSLEngine on
SSLCertificateFile /etc/ssl/apache2/certificate.com.crt
SSLCertificateKeyFile /etc/ssl/apache2/certificate.key
</VirtualHost>
#Another virtual host with another site
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /var/www/myothersite
ServerName www.myothersite.com
ServerAlias myothersite.com
<Directory /var/www/myothersite>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost>
但是,我无法通过 SSL 访问该网站。但我可以通过 http 访问它。
Apache 重新启动时显示以下警告:
NameVirtualHost *:443 没有 VirtualHosts NameVirtualHost *:80 没有 VirtualHosts
这很令人困惑,因为端口 80 有大约 10 个虚拟主机。
答案1
在我看来,您没有为虚拟主机指定 443 端口或 80 端口。因此,所有内容都流向为 SSL 配置的虚拟主机。因此,http 流量不被接受,因为它被配置为仅接受 SSL。试试这个
NameVirtualHost *:80
<VirtualHost *:80>
ServerName blah
DocumentRoot /var/www/html/
</VirtualHost>
<VirtualHost *:443>
...
- your config here -
...
</VirtualHost>
您甚至可以对在端口 80 上运行的虚拟主机进行重定向。也许是这样的:
Redirect permanent / https://<URL>