我们主要讨论一下SAN证书(Subject Alternative Name)在服务器上的配置,确保每个域名都能正确响应。
由于SAN证书包含多个域名,我们需要在服务器配置中正确地处理这些域名,以便当客户端通过不同的域名访问时,服务器能提供正确的SSL证书并返回相应的内容。以下是一些常见服务器的配置示例和注意事项。
一、Nginx配置示例
假设我们有一个SAN证书,包含了以下域名:
example.com
www.example.com
example.net
www.example.net
我们需要确保Nginx配置中,每个域名都能被正确识别,并且使用同一个证书。
示例配置:
我们可以为每个域名设置一个server块,但这样会导致重复配置。另一种方法是使用一个server块来监听所有域名,并根据域名进行不同的处理。
但是,请注意:如果你有不同的根目录或配置,可能需要分开配置。这里假设所有域名都指向同样的内容。
如果内容相同,可以这样配置:
nginx
server {
listen 443 ssl;
server_name example.com www.example.com example.net www.example.net;
ssl_certificate /path/to/your/san_certificate.crt;
ssl_certificate_key /path/to/your/private.key;
其他SSL配置...
root /var/www/html;
index index.html;
}
如果每个域名需要不同的内容,则需要多个server块:
nginx
为example.com和www.example.com配置
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /path/to/your/san_certificate.crt;
ssl_certificate_key /path/to/your/private.key;
root /var/www/example.com;
index index.html;
}
为example.net和www.example.net配置
server {
listen 443 ssl;
server_name example.net www.example.net;
ssl_certificate /path/to/your/san_certificate.crt;
ssl_certificate_key /path/to/your/private.key;
root /var/www/example.net;
index index.html;
}
注意:在同一个IP地址上,Nginx通过SNI(Server Name Indication)来区分不同的域名。现代浏览器都支持SNI。
二、Apache配置示例
同样,假设我们有相同的四个域名。
如果内容相同,可以配置一个VirtualHost:
apache
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com example.net www.example.net
SSLEngine on
SSLCertificateFile /path/to/your/san_certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
DocumentRoot /var/www/html
</VirtualHost>
如果内容不同,则需要多个VirtualHost:
apache
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /path/to/your/san_certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
DocumentRoot /var/www/example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.net
ServerAlias www.example.net
SSLEngine on
SSLCertificateFile /path/to/your/san_certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
DocumentRoot /var/www/example.net
</VirtualHost>
三、Tomcat配置示例(使用APR或NIO连接器)
在Tomcat中配置SSL,并在Host元素中指定不同的域名。
首先,在server.xml中配置Connector使用SSL,并指定证书:
xml
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateFile="/path/to/your/san_certificate.crt"
certificateKeyFile="/path/to/your/private.key"
type="RSA" />
</SSLHostConfig>
</Connector>
然后,为每个域名配置Engine和Host。注意,Tomcat的Engine的defaultHost属性指定默认主机。
xml
<Engine name="Catalina" defaultHost="example.com">
<Host name="example.com" appBase="webapps-example-com"
unpackWARs="true" autoDeploy="true">
<Alias>www.example.com</Alias>
</Host>
<Host name="example.net" appBase="webapps-example-net"
unpackWARs="true" autoDeploy="true">
<Alias>www.example.net</Alias>
</Host>
</Engine>
四、负载均衡器配置(以HAProxy为例)
HAProxy中配置多个域名的SSL证书,可以使用同一个证书文件(SAN证书)来承载多个域名。
haproxy
frontend https_frontend
bind *:443 ssl crt /path/to/your/san_certificate.pem
注意:HAProxy的crt参数需要将证书和密钥合并到一个pem文件中,顺序是证书、中间证书(如果有)、私钥
根据域名进行后端路由
acl host_example_com hdr(host) -i example.com
acl host_www_example_com hdr(host) -i www.example.com
acl host_example_net hdr(host) -i example.net
acl host_www_example_net hdr(host) -i www.example.net
use_backend backend_example_com if host_example_com or host_www_example_com
use_backend backend_example_net if host_example_net or host_www_example_net
backend backend_example_com
server server1 192.168.1.10:80
backend backend_example_net
server server1 192.168.1.11:80
五、确保HTTP重定向到HTTPS(可选但推荐)
对于每个域名,我们通常希望将HTTP请求重定向到HTTPS。
在Nginx中,可以为每个域名配置一个80端口的server块进行重定向:
nginx
server {
listen 80;
server_name example.com www.example.com example.net www.example.net;
return 301 https://$server_name$request_uri;
}
在Apache中类似:
apache
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com example.net www.example.net
Redirect permanent / https://example.com/
</VirtualHost>
六、验证配置
配置完成后,使用以下命令验证配置是否正确:
Nginx: nginx -t
Apache: apachectl configtest
然后使用浏览器访问每个域名,确保证书被正确展示,并且内容符合预期。
七、注意事项
证书链完整:确保服务器配置中包含了完整的证书链(包括中间证书),否则某些客户端可能无法验证。
私钥安全:确保私钥文件权限适当,避免泄露。
定期更新:SAN证书包含的域名较多,一旦更新,需要及时替换服务器上的证书。
用户通过以上配置,就可以确保用户多域名SSL证书中的每个域名都能被正确响应。