分享

单个Tomcat配置多个域并配置多个证书

 aaie_ 2017-11-05

近日,帮一个朋友配置一台服务器,在该服务器上启动一个Tomcat运行两个应用,分别对应两个域名:www. 和 www. ,对于http协议(80端口),只要配置Tomcat的虚拟主机就可以了。

      但朋友为了数据的安全性,分别为每个域名购买了一个CA证书。这就要求在一个Tomcat上配置两个证书。在网上搜了好久,没见有相同的案例。只查到有人说了两种办法:

一、两个域名使用不同的HTTPS端口,比如:www.使用443端口,www. 使用8443端口,这种方式对于测试可以,但用于生产环境,要求普通用户在输入地址时还要输入端口8443,不方便不说,有些用户还不懂。所以这种方案只能暂时放弃。

二、使用两个公网IP,每个域名对应一个IP,这样就可以使每个域名都使用443作为HTTPS的端口,方便用户使用。但没有查到实际的配置案例。

      既然没有案例,那就自己动手,开始尝试。经过N次尝试之后,终于配置成功。为了防止忘记,也为了方便别人,把配置文件贴出来。为了减少篇幅,把大部分注释删除了。

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <Server port="8005" shutdown="SHUTDOWN">  
  4.   
  5.   <!-- Comment these entries out to disable JMX MBeans support used for the administration web application -->  
  6.   <Listener className="org.apache.catalina.core.AprLifecycleListener" />  
  7.   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />  
  8.   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  
  9.   <Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>  
  10.   
  11.   <!-- Global JNDI resources -->  
  12.   <GlobalNamingResources>  
  13.   
  14.     <!-- Test entry for demonstration purposes -->  
  15.     <Environment name="simpleValue" type="java.lang.Integer" value="30"/>  
  16.   
  17.     <!-- Editable user database that can also be used by  
  18.          UserDatabaseRealm to authenticate users -->  
  19.     <Resource name="UserDatabase" auth="Container"  
  20.               type="org.apache.catalina.UserDatabase"  
  21.        description="User database that can be updated and saved"  
  22.            factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  23.           pathname="conf/tomcat-users.xml" />  
  24.   
  25.   </GlobalNamingResources>  
  26.   
  27.   <!-- Define the Tomcat Stand-Alone Service -->  
  28.   <Service name="Catalina">  
  29.   
  30.     <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->  
  31.     <Connector port="80" maxHttpHeaderSize="8192"  
  32.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  33.                enableLookups="false" redirectPort="8443" acceptCount="100"  
  34.                connectionTimeout="20000" disableUploadTimeout="true" />  
  35.   
  36.     <!-- Define a SSL HTTP/1.1 Connector on port 443 -->  
  37.     <Connector port="443" maxHttpHeaderSize="8192"  
  38.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  39.                enableLookups="false" disableUploadTimeout="true"  
  40.                acceptCount="100" scheme="https" secure="true"  
  41.                clientAuth="false" sslProtocol="TLS"  
  42.     keystoreFile  ="D:/certs/my_keystore.jks" keystorePass="www.my" keystoreType="JKS"   
  43.     truststoreFile="D:/certs/my_keystore.jks" truststorePass="www.my" truststoreType="JKS"  
  44.     address="xxx.xxx.2.83"  
  45.                />  
  46.   
  47.     <Connector port="443" maxHttpHeaderSize="8192"  
  48.                maxThreads="150" minSpareThreads="25" maxSpareThreads="75"  
  49.                enableLookups="false" disableUploadTimeout="true"  
  50.                acceptCount="100" scheme="https" secure="true"  
  51.                clientAuth="false" sslProtocol="TLS"  
  52.     keystoreFile  ="D:/certs/my_keystore.jks" keystorePass="www.my" keystoreType="JKS"   
  53.     truststoreFile="D:/certs/my_keystore.jks" truststorePass="www.my" truststoreType="JKS"  
  54.     address="xxx.xxx.2.81"  
  55.                />  
  56.   
  57.     <!-- Define an AJP 1.3 Connector on port 8009 -->  
  58.     <Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />  
  59.   
  60.         <!-- Define the top level container in our container hierarchy -->  
  61.         <Engine name="Catalina" defaultHost="localhost">  
  62.   
  63.       <!-- This Realm uses the UserDatabase configured in the global JNDI  
  64.            resources under the key "UserDatabase".  Any edits  
  65.            that are performed against this UserDatabase are immediately  
  66.            available for use by the Realm.  -->  
  67.       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  resourceName="UserDatabase"/>  
  68.   
  69.       <!-- Define the default virtual host  
  70.            Note: XML Schema validation will not work with Xerces 2.2.  
  71.        -->  
  72.       <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">  
  73.       </Host>  
  74.   
  75.       <Host name="xxx.xxx.2.81" appBase="D:/mydomain2/webapp" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">  
  76.     <Alias>my</Alias>  
  77.     <Alias>www.my</Alias>  
  78.       </Host>  
  79.   
  80.       <Host name="xxx.xxx.2.83" appBase="D:/mydomain1/webapp" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">  
  81.     <Alias>my</Alias>  
  82.     <Alias>tax.my</Alias>  
  83.     <Alias>www.my</Alias>  
  84.     <Alias>www.mydomain1.cn</Alias>  
  85.     <Alias>mydomain1.cn</Alias>  
  86.       </Host>  
  87.   
  88.   
  89.     </Engine>  
  90.   
  91.   </Service>  
  92.   
  93. </Server>  

 注意两个Port="443"的Connector配置,最后面的address参数是关键,如果不加address,那么Tomcat将会报错,说443端口已被使用。其他的配置信息,网络上都能找到例子或说明,就不多做说明了。

http://www./topic/554238

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多