一台tomcat 配置多http域名或者多端口号多应用

虚拟主机提供web服务

一、支持多域名访问

在tomcat/conf目录下

修改server.xml

几个域名就添加几个host

name=域名

appBase = 应用文件名

      

        

      

备注:tomcat 启动,所有应用都会重新启动

二、支持多端口号访问


 

  
  
  
  
  
  
  
  
  
 
  
    
  
 
 
  
 
    
 
	
    
      
        
      
 
      
		
      
    
  
 
   
    
 
    
        
          
		
	
  
  
  
    
    
        
          
		
	
  

1、按照上面的配置,启动tomcat,可以发现:
1)启动tomcat后,在tomcat配置目录:/usr/local/tomcat/conf下,除了默认的Catalina文件夹外,还会生成web1和web2两个文件夹。原因是Engine组件中,指定了所用的web1和web2。当然,我们可以将不同service组件的Engine name都指定成Catalina。

2)启动tomcat后,在tomcat目录:/usr/local/tomcat下,除了默认的webapps文件夹外,还会生成webapp1和webapp2两个目录。原因是Host组件中,指定了appBase伟webapp1和webapp2.当然,我们可以将不同service组件的Host appBase指定成默认的webapps。

2、配置说明:

1)appBase是虚拟主机存放webapp的目录,它可以是相对路径,也可以是绝对路径。如果是相对路径,则相对于$CATALINA_HOME,严格并准确地说是CATALINA_BASE。

2)path是URI的匹配路径,相当于nginx的location后的路径。tomcat要求每个虚拟主机必须配置一个空字符串的path,该条context作为URI无法被明确匹配时的默认context,它相当于nginx中location / {}的作用。

3)docBase则是每个webapp的存放目录(或者是已归档的war文件),它可以是相对路径,也可以是绝对路径,提供相对路径时它相对于appBase。该目录一般在appBase的目录下,但并不规定一定要放在appBase下。对于web服务来说,它相当于nginx的root指令,但对于webapp来说,一个context就相当于一个webapp,而docBase正是webapp的路径。

3、注意:

1)如果多个项目都用了spring框架,那么在配置成一个tomcat多端口多应用时,可能会报错:
 

13-Apr-2018 12:52:08.716 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart 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' = [/data/ttbrain-log-admin/] instead of [/data/Disu/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
	at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:151)
	at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:116)
	at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
	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)

原因是:部署在同一容器中的Web项目,要配置不同的,不能重复,否则就会报上面类似的错误。

2)解决方法:在web.xml中加上:


  webAppRootKey
  webapp.root

"webapp.root"这个字符串可以随便写任何字符串。如果不配置默认值是"webapp.root"。可以用System.getProperty("webapp.root")来动态获项目的运行路径。
3)加载方式:

Spring通过org.springframework.web.util.WebAppRootListener 这个监听器来运行时的项目路径。但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能
一般配置类型下面的例子:
 

  
  
    log4jConfigLocation  
    WEB-INF/conf/log4j.properties  
     
  
  
    log4jRefreshInterval  
      3000  
   
  
  
    org.springframework.web.util.Log4jConfigListener  
   

在log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log 就可以在运行时动态的找出项目的路径

 

参考:https://blog.csdn.net/liuxiao723846/article/details/79940428

参考:https://www.cnblogs.com/f-ck-need-u/p/8120008.html

你可能感兴趣的:(tomcat)