首先带着疑问html
问题一:去哪配置?
核心配置在Tomcat目录下conf/
目录下的server.xml
文件中java问题二:怎么配置?web
以下apache
<!-- Server 根元素,建立⼀个Server实例,⼦标签有 Listener、GlobalNamingResources、Service --> <Server> <!--定义监听器--> <Listener/> <!--定义服务器的全局JNDI资源 --> <GlobalNamingResources/> <!-- 定义⼀个Service服务,⼀个Server标签能够有多个Service服务实例 --> <Service/> </Server>
说明Tomcat来监听port端口来执行关闭浏览器
<!-- port:关闭服务器的监听端⼝ shutdown:关闭服务器的指令字符串 --> <Server port="8005" shutdown="SHUTDOWN"> <!-- 以⽇志形式输出服务器 、操做系统、JVM的版本信息 --> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <!-- 加载(服务器启动) 和 销毁 (服务器停⽌) APR。 若是找不到APR库, 则会输出⽇志, 并不影响 Tomcat启动 --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <!-- 避免JRE内存泄漏问题 --> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <!-- 加载(服务器启动) 和 销毁(服务器停⽌) 全局命名服务 --> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <!-- 在Context停⽌时重建 Executor 池中的线程, 以免ThreadLocal 相关的内存泄漏 --> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html GlobalNamingResources 中定义了全局命名服务 --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <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> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> ... </Service> </Server>
Listenertomcat
GlobalNamingResources (中定义了全局命名服务)安全
Service (以下)服务器
以上标签Listener
、GlobalNamingResources
通常状况下保持默认便可,不须要而外操做,重点在于Service 标签app
<Service name="Catalina"> ... </Service>
该标签⽤于建立 Service 实例,默认使⽤ org.apache.catalina.core.StandardService
。 默认状况下,Tomcat 仅指定了Service 的名称, 值为 "Catalina"。webapp
用来定义共享线程池的。
默认是被注释掉的,且属性不全。那么该如何使用呢?
默认状况下,Service 并未添加共享线程池配置。 若是咱们想添加⼀个线程池, 能够在
<Executor name="commonThreadPool" namePrefix="thread-exec-" maxThreads="200" minSpareThreads="100" maxIdleTime="60000" maxQueueSize="Integer.MAX_VALUE" prestartminSpareThreads="false" threadPriority="5" className="org.apache.catalina.core.StandardThreadExecutor"/>
org.apache.catalina.core.StandardThreadExecutor
。若是想使⽤⾃定义线程池⾸先须要实现 org.apache.catalina.Executor
接⼝Connector 标签⽤于建立连接器实例默认状况下,server.xml
配置了两个连接器,⼀个⽀持HTTP协议,⼀个⽀持AJP协议,⼤多数状况下,咱们并不须要新增连接器配置,只是根据须要对已有连接器进⾏优化。
<!--org.apache.coyote.http11.Http11NioProtocol , ⾮阻塞式 Java NIO 连接器--> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
maxThreads
、minSpareThreads
等属性配置内部线程池。可使用共享线程池:
<Connector port="8080" protocol="HTTP/1.1" executor="commonThreadPool" maxThreads="1000" minSpareThreads="100" acceptCount="1000" //每每和maxThreads保持一致 maxConnections="1000" //每每和maxThreads保持一致 connectionTimeout="20000" compression="on" //要不要启动gzip压缩 compressionMinSize="2048" //压缩处理的最小大小,超过此大小才压缩 disableUploadTimeout="true" //单独的给servlet放宽超时时间 redirectPort="8443" URIEncoding="UTF-8" />
能够看到
Connector
标签和Executor
都有maxThreads
和minSpareThreads
标签。每个Connector均可以本身定义一个线程池,若是每一个Connector都本身定义线程池就浪费了。因此为何不用一个呢?因而就有了Executor标签来定义一个共享线程池。
Engine 表示 Servlet 引擎
<Engine name="Catalina" defaultHost="localhost"> ... </Engine>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> ... </Host>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
当请求来临时,日志存放起来,
directory
值的目录<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
hostname是localhost,appBase是webapps,去webapps文件夹下找应用程序文件夹,没有指定,默认就去ROOT文件夹下:
www.abc.com
,www.def.com
。appBase分别为webapps
、webapps2
能够看到localhost:8080访问不到了,由于这个虚拟主机被咱们修改了
而abc和def都可以找到。同时def.com的Home也被修改成Home-webapps2了。
用于配置一个Web应用——一个虚拟主机下能够配置多个应用
<Host name="www.abc.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- docBase:Web应⽤⽬录或者War包的部署路径。能够是绝对路径,也能够是相对于 Host appBase的 相对路径。 path:Web应⽤的Context 路径。若是咱们Host名为localhost, 则该web应⽤访问的根路径为: http://localhost:8080/web_demo。 --> <Context docBase="E://dengxhh" path="/dengxh"></Context> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
刚才咱们访问的是
http://www.abc.com:8080
,那么如今我想输入http://www.abc.com:8080/dengxh
,dengxh所表明的位置是个人磁盘上的某个文件夹,这个文件夹里放的个人程序资源。如何作?
为了方便演示,咱们就拿ROOT中的程序作例子,把webapps中的ROOT文件夹复制到E盘根目录,取名为dengxhh
在Host标签中,增长 <Context docBase="E://dengxhh" path="/dengxh"></Context>
重启Tomcat,访问www.abc.com/dengxh/ ,成功跳转了。
如此操做就能够在一个虚拟主机中,配置多个应用,只须要在host标签中配置多个Context标签,区分的时候只须要在虚拟主机后跟上Context的path属性的值便可找到对应的资源。
例如:Host name="www.abc.com" ,Context path ="dengxh" ,那么浏览器中输入 www.abc.com/dengxh
便可。
思惟导图在线观看地址:https://www.processon.com/view/link/5fe4da565653bb054783ca6e