作者:简了个书1993
转自:https://www.jianshu.com/p/2b4a587db5cb
同一Tomcat下配置多虚拟主机,资料查了好久,按照别人说的配置,配置好了,但是发现了一个问题,Safari、Chrome都可以成功打开,唯独Firefox说配置有问题,无法验证证书!以为Firefox有问题,然后升级版本之后依然不行!又在第三方网站上查询域名,说证书不是可信的签发者,也就是说无法验证证书的签发者!
但是这里有个问题,当只配置单个虚拟主机的时候是没问题的,配置多个就不行了!然后查看了官方文档,原来是需要配置CA证书才能验证,然后就把签发者的CA证书配置上了,终于成功了!
Connector部分属性配置参考
http://tomcat.apache.org/tomcat-9.0-doc/config/http.html
webapps目录结构如下
webapps/
├───domain
│ └─ROOT
├───x1
│ └─ROOT
└───x2
└─ROOT
️ 网站要放在ROOT目录下才能被识别
编辑 conf/server.xml
1.找到 443端口位置 做如下修改
未配置APR的 配置如下
<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" URIEncoding="UTF-8" defaultSSLHostConfigName="domain.com">
<SSLHostConfig hostName="domain.com" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/domain.key"
certificateFile="conf/domain.crt"
type="RSA" />
</SSLHostConfig>
<SSLHostConfig hostName="x1.domain.top" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/x1.key"
certificateFile="conf/x1.crt"
type="RSA" />
</SSLHostConfig>
<SSLHostConfig hostName="x2.domain.top" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/x2.key"
certificateFile="conf/x2.crt"
type="RSA" />
</SSLHostConfig>
</Connector>
1.配置了APR的 协议名更改为 "org.apache.coyote.http11.Http11AprProtocol"# 2.使用HTTP 2.0 需要在Connector添加子节点
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" /># tomcat 9.0 已存在默认配置
找到Connector协议为org.apache.coyote.http11.Http11AprProtocol
取消注释,相应修改即可
<Connector port="443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" URIEncoding="UTF-8" defaultSSLHostConfigName="domain.com">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig hostName="domain.com" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/domain.key"
certificateFile="conf/domain.crt"
type="RSA" />
</SSLHostConfig>
<SSLHostConfig hostName="x1.domain.top" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/x1.key"
certificateFile="conf/x1.crt"
type="RSA" />
</SSLHostConfig>
<SSLHostConfig hostName="x2.domain.top" caCertificateFile="conf/CA证书.pem">
<Certificate certificateKeyFile="conf/x2.key"
certificateFile="conf/x2.crt"
type="RSA" />
</SSLHostConfig>
</Connector>
defaultSSLHostConfigName:当存在多个虚拟主机时 必须指定一个做为默认值# CA证书.pem 是你的https证书签发机构的中级证书
2.找到 Host 位置配置虚拟主机,各主机的根目录全部放在webapps内,这样不用开启多个服务,各虚拟主机共用80端口
<Engine name="Catalina" defaultHost="domain.com">
<Host name="domain.com" appBase="webapps/domain"
unpackWARs="true" autoDeploy="true">
<Alias>www.domain.com</Alias>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<!-- x1 -->
<Host name="x1.domain.com" appBase="webapps/x1"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<!-- x2 -->
<Host name="x2.domain.com" appBase="webapps/x2"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host></Engine>
保存 重启tomcat服务
至此 大功告成!