有没有办法为域和所有子域设置SSL?

所以,我有一个有许多子域的域。 比方说example.coma.example.comb.example.comc.example.com等。

我想要从相同的IP地址在同一台服务器上托pipe他们。

我有我的通配符证书和我的基础域authentication, star_example_com.crtexample_com.crt

我试图设置我的Apache服务器来支持域和子域的SSL,但似乎无法得到它的工作。 域是好的,但子域不是。

这是我的configuration:

 NameVirtualHost *:443 # To support SNI, although, apparently, this directive is deprecated SSLStrictSNIVHostCheck off <VirtualHost example.com:443> SSLEngine On SSLCertificateFile /etc/apache2/ssl/example_com.crt SSLCertificateKeyFile /etc/apache2/ssl/example_com.key SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> <VirtualHost *:443> ServerAlias *.example.com SSLEngine On SSLCertificateFile /etc/apache2/ssl/star_example_com.crt SSLCertificateKeyFile /etc/apache2/ssl/star_example_com.key SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost> 

我在这里错过了很明显的东西吗 我所要做的就是确保正确的SSL证书被用于域名和子域名…一切都是一样的(这是一个WordPress多站点,所以最终都到了相同的地方。

要清楚,我访问子域名时出现错误,因为我没有得到通配符证书。

为了进一步明确,我有超过80个子域名。 我想避免为每个人创build一个VirtualHost,如果可能的话。

任何指针?

我没有足够的评价。 所以我会回答。 首先你应该有子域和域。 这取决于顺序,所以首先应该configuration子域,最后是整个根域。 我告诉你我的configuration是完美的:

/etc/apache2/sites-available/example.conf:

 # ====================SSL======================== <IfModule mod_ssl.c> ## a.example.com <VirtualHost *:443> ServerAdmin [email protected] ServerName a.example.com ServerAlias *.a.example.com DocumentRoot /home/a.example.com/www/public_html ErrorLog /home/a.example.com/www/logs/ssl_error.log CustomLog /home/a.example.com/www/logs/ssl_access.log combined SSLEngine on SSLCertificateFile /home/a.example.com/www/ssl/a.example.com.crt SSLCertificateKeyFile /home/a.example.com/www/ssl/a.example.com.key SSLCertificateChainFile /home/a.example.com/www/ssl/a.example.com.ca-bundle.crt <Directory /home/a.example.com/www/public_html> Options FollowSymLinks -MultiViews AllowOverride All Order allow,deny Allow from all Satisfy Any </Directory> </VirtualHost> ## /a.example.com ## example.com <VirtualHost *:443> ServerAdmin [email protected] ServerName example.com ServerAlias *.example.com DocumentRoot /home/example.com/www/public_html/ <Directory /home/example.com/www/public_html/> Options FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog /home/example.com/www/logs/ssl_error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog /home/example.com/www/logs/ssl_access.log combined # SSL Engine Switch: # Enable/Disable SSL for this virtual host. SSLEngine on SSLCertificateFile /home/example.com/www/ssl/example.com.crt SSLCertificateKeyFile /home/example.com/www/ssl/example.com.key SSLCertificateChainFile /home/example.com/www/ssl/example.com.ca-bundle.crt # SSL Engine Options: <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> # SSL Protocol Adjustments: 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> ## /example.com </IfModule> # ====================/SSL======================== 

请记住,您应该将configuration与您的环境进行比较,因此在example.com之前添加更多域名(如a.example.com)。 我希望我的回答能以某种方式帮助你。

我的回答是太长的评论,所以这就是为什么我发布新的答案。 你可以只添加ServerAlias,而不是整个VirtualHost你可以指定更多的ServerAliases。 但是我明白你想要什么,所以如果你不想在每次添加新的wp站点的时候触摸apacheconfiguration的ServerAlias文件,那么你可以试试VirtualDocumentRoot。 我不太了解wordpress多站点结构的样子。 比如你有这样的结构:

 /var/www/html/ = main WP site example.com /var/www/html/site1/ = site1.example.com /var/www/html/a/ = a.example.com 

等等。 我假设你想翻译这样的请求:

 site1.example.com > example.com/site1/ a.example.com > example.com/a/ 

如果这是你的情况,你应该尝试用VirtualDocumentRootreplaceDocumentRoot:

 ServerName example.com ServerAlias *.example.com VirtualDocumentRoot /var/www/html/%0 

您需要启用vhost_alias:

 a2enmod vhost_alias 

%0使整个文件夹的名称为“ServerAlias”,如果wordpress使/var/www/html/site1.example.com/那么你必须用%1replace%0,这里是规则表(你也可以合并它们):

 0 the whole name 1 the first part 2 the second part -1 the last part -2 the penultimate part 2+ the second and all subsequent parts -2+ the penultimate and all preceding parts 1+ and -1+ the same as 0 

如果你尝试VirtualDocumentRoot记得注释掉DocumentRoot,否则Apache可能无法启动。 这里是vhost_alias的文档。

我最终通过创build带有SubjectAltName(SAN)证书的证书来解决此问题。

创buildCSR有一点涉及。 你必须像这样创build一个configuration文件:

 [req] distinguished_name = req_distinguished_name x509_extensions = v3_req prompt = no [req_distinguished_name] C = US ST = <your state> L = <your city> O = <your company> CN = <your IP address> [v3_req] keyUsage = keyEncipherment, dataEncipherment extendedKeyUsage = serverAuth subjectAltName = @alt_names [alt_names] DNS.1 = example.com DNS.2 = *.example.com 

然后传入您的CSR请求,例如:

 openssl req -new -out san_example_com.csr -key san_example_com.key -config san_req.cnf