我的SSL证书只能与“www.example.com”一起使用,而不能与“example.com”

我购买了对www.example.com和example.com均有效的证书。 我的服务器只能用“www.example.com”(ssl工作正常),但是在浏览器上input“example.com”时什么也没有显示。 这里是我的Apache文件configuration基本上是相同的虚拟主机两次,但与不同的ServerName

Define APACHE_LOG_DIR /var/log/apache2 Define SSLCERTIFICATE /etc/apache2/ssl/mycertificate.crt Define SSLKEY /etc/apache2/ssl/mykey.key <IfModule mod_ssl.c> <VirtualHost *:443> ServerAdmin [email protected] ServerName www.example.com DocumentRoot /var/www/site2/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile ${SSLCERTIFICATE} SSLCertificateKeyFile ${SSLKEY} <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory "/var/www/site2"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order Allow,Deny Allow from all </Directory> BrowserMatch "MSIE [2-6]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 # MSIE 7 and newer should be able to use keepalive BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown </VirtualHost> # Same documetn root for example.com (without www) <VirtualHost *:443> ServerAdmin [email protected] ServerName example.com DocumentRoot /var/www/site2/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile ${SSLCERTIFICATE} SSLCertificateKeyFile ${SSLKEY} <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory "/var/www/site2"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order Allow,Deny Allow from all </Directory> BrowserMatch "MSIE [2-6]" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0 # MSIE 7 and newer should be able to use keepalive BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown </VirtualHost> </IfModule> 

附加信息:

  • 我的网站托pipe在Azure虚拟机(Ubuntu 14.04 LTS)
  • 该域名来自goDaddy
  • 还有另一个httpconfiguration文件(端口80)

由于您的证书对于example.comwww.example.com都是有效的,并且这两个域都被configuration为使用相同的目录,所以我没有理由认为它们是独立的VirtualHost。 我会删除第二个VirtualHost,并改变第一个看起来像这样:

  <VirtualHost *:443> ServerAdmin [email protected] ServerName www.example.com ServerAlias example.com [the rest of the config should look the same as it does in your post] </VirtualHost> 

之所以没有看到没有www的版本,是因为你使用两个不同域名的IP *:443两个通配符。 Apache只是使用第一个使用证书。 从Apache文档[1]:

通过SSL使用命名虚拟主机的问题是,命名虚拟主机依赖于知道请求的主机名,并且在build立SSL连接之前,不能读取请求。 那么,普通的行为就是使用默认虚拟主机中的configuration来设置SSL连接,以获取连接的地址。

要使用通配符,则需要每个证书一个IP。 Apache将使用每个虚拟IPredirect传入的SSLencryption请求。 要做到这一点:

  1. 添加一个虚拟IP到接口(如果我们假设它是eth0,添加一个别名)
  2. 更新文件ports.conf ,添加两个条目:
    • NameVirtualHost IP1:443
    • NameVirtualHost IP2:443
  3. 更新你的vHost,看起来像这样:

    网站与www

    ServerName http://www.site.com

    网站w / o www

    ServerName site.com

编辑
您也可以将所有传入的匹配从“site.com”redirect到“www.site.com”。 为此,请删除site.com的vHost并添加以下行:ServerAlias site.com

这样做会指示Apache为这两个域使用相同的vHost。 如果您想将所有传入的匹配( http://example.com和http://www.example.com )redirect到安全主域( https://www.example.com ),请考虑添加以下内容指令:

 RewriteEngine On RewriteCond %{HTTPS} off # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

通过Serverfault规则将site.comreplace为example.com

[1] https://wiki.apache.org/httpd/NameBasedSSLVHostsWithSNI

正如你想使用SNI,你必须启用它。 检查一下

NameVirtualHost *:443

在您的configuration中可用

但我认为应该更好地添加一个serverAlias和相同的根目录。 你可以通过mod_rewrite来select差异。

最后,我通过像Jenny D所build议的那样进行pipe理,并从mypage.comredirect到www.mypage.com,并将goDaddyconfiguration中的www www CName更改为我的azure虚拟机地址。

因此在这种情况下,修改.htaccess和使用SNI都是不必要的。 不知道这是最好还是理想的configuration,但至less是工作。