【tomcat】不一样域名解析到同一tomcat不一样项目上

 

 

问题:html

  1. 有多个域名,想输入的每一个域名只能访问其中的一个项目前端

     2. 这些项目都部署在同一个tomcat上的java

  

   解决步骤:nginx

        1.首先把全部域名都解析到这台服务器上,解析时只能填写ip地址,不能指定端口的,默认访问解析IP的80端口web

    2.把web项目部署在tomcat安装目录下,须要和默认的webapps目录平级,而且直接将web项目打包为ROOT.war 启动时会解压到ROOT文件夹redis

     目录结构以下:spring

                     

 

     3. 修改tomcat conf目录下 server.xml 文件以下(或者直接用下面的代码替换,端口已经改成80了) apache

      

<?xml version='1.0' encoding='utf-8'?>
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>

    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">

    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
            
      <!--
        若是是本地测试请修改
        C:\Windows\System32\drivers\etc\hosts 文件
        # For example:
        127.0.0.1 localhost
        127.0.0.1       localhost
        127.0.0.1       www.site1.com
        127.0.0.1       www.site2.com
        127.0.0.1       site1.com
        127.0.0.1       site2.com
        
        若是是已注册好的域名请解析www,@到tomcat所在服务器ip
        
        端口8080 请修改成80,不然须要在域名后面加端口
        
        下面每一个项目都解析了两次,是为了用户在输入网址时,无论有没有www.都能正常访问
      -->
      <Host name="site1.com" appBase="site1"
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
     </Host>
     <Host name="www.site1.com" appBase="site1"
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
     </Host>
        
     <Host name="site2.com" appBase="site2"
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
     </Host>
     <Host name="www.site2.com" appBase="site2"
           unpackWARs="true" autoDeploy="true"
           xmlValidation="false" xmlNamespaceAware="false">
     </Host>
    </Engine>
  </Service>
</Server>

注意: 本地测试请修改host文件,请看上面注释windows

 

启动tomcat后 无论是输入 www.site1.com 或者 site1.com都能正常访问了!tomcat

 

 

搭建简单项目能够按照上面方式进行,可是若是项目比较庞大或者访问较多, 请不要使用此方式, 应该采ngix前端分发到不一样端口上

配置请参看 个人另外一篇文章 

windows 环境下nginx + tomcat群 + redis 实现session共享

 

 

2016年1月29日22:02更新

为了测试两个标准的spingmvc+hibernate+shiro web项目到底启动须要消耗多少内存,又发现了些问题

 

由于要部署两个项目,按照上面的方法, 我很懒 直接拷贝一个打好的war包到site1目录和site2目录【注意  这两个war包是如出一辙的】

而后启动tomcat

..............................................

第一个先解压的site1 正常

第二个启动site2   提示初始化完成,紧接着又提示closing。 错误提示只有一个   start error!!!!!!!!!!!!!!

 

start error!!!!   到底啥错没有更详细的了,查了些资料为了要查看更详尽的信息须要进行这一步操做

就是在WEB-INF/classes目录下方式一个logging.properties文件,内容以下:

handlers = org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

org.apache.juli.FileHandler.level = FINE
org.apache.juli.FileHandler.directory = ${catalina.base}/logsdebug
org.apache.juli.FileHandler.prefix = error-debug.

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

再启动,  去tomcat 的 logsdebug目录下查看启动信息  或者 直接看控制台启动日志,如今就能看到相信信息了

错误提示为:

严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [D:\dev\apache-tomcat-7.0.11\site1\ROOT\] instead of [D:\dev\apache-tomcat-7.0.11\webapps\ROOT\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
    at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:150)
    at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
    at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:46)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4681)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5184)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5179)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

 

 

看见了 webAppRootKey  这个就容易了,为啥呢, 由于我知道这个啥意思,不懂的百度下

而且错误信息也说得很明确了  看几个关键字就行了     web.xml   unique value

由于咱们两个war包彻底一致,至关于在同一个tomcat下启动了两个同名的项目,这个不容许

 

须要进行以下配置,为每个项目制定一个不一样的 webAppRootKey值,由于项目都又log4j

直接添加参数就行了

 

<display-name>site1</display-name>
    <description>site1.com</description>
    
    <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>site1</param-value>
    </context-param>

 

直接写在最前面就行了,   每一个项目的 webAppRootKey 必定要不同【同一个tomcat下】,不然为默认值 webapp.root, 而后我又没有设置环境变量  因此直接改

 

发现了这个问题,其实能够之后再写xml时候,不论会不会在同一个tomcat下发布,我建议你们都指定一个值,好比当前时间戳精确到毫秒或者uuid.....

 

 

天天进步一点点

相关文章
相关标签/搜索