Tomcat 端口配置,及原理詳解
如果想深入了解tomcat的各個端口及配置,建議通讀文章,對初學者十分有利,整理自網絡。
1 tomcat 文件配置詳細說明
tomcat服務器需配置三個端口才能啟動,安裝時默認啟用了這三個端口,當要運行多個tomcat服務時需要修改這三個端口,不能相同。端口配置路徑為tomcat\ conf\service.xml
- 修改Shutdown端口(默認為8005端口)
<Server port="8005" shutdown="SHUTDOWN">
- 1
- 修改http訪問端口(默認為8080端口)
負責建立HTTP連接。在通過瀏覽器訪問Tomcat服務器的Web應用時,使用的就是這個連接器。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
- 1
- 2
- 3
- AJP協議訪問端口(默認為8009端口)
負責和其他的HTTP服務器建立連接。在把Tomcat與其他HTTP服務器集成時,就需要用到這個連接器。
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- 1
- 2
- Web客戶訪問Tomcat服務器上JSP組件的兩種方式如圖所示。
配置文件$CATALINA_HOME/conf/server.xml的說明該文件描述了如何啟動Tomcat Server
<!----------------------------------------------------------------------------------------------->
<!-- 啟動Server ***在端口8005處等待關閉命令*** 如果接受到"SHUTDOWN"字符串則關閉服務器 -->
<Server port="8005" shutdown="SHUTDOWN" debug="0">
<!-- Listener ??? 目前沒有看到這里 -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" debug="0"/>
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" debug="0"/>
<!-- Global JNDI resources ??? 目前沒有看到這里,先略去 -->
<GlobalNamingResources>
... ... ... ...
</GlobalNamingResources>
<!-- Tomcat的Standalone Service Service是一組Connector的集合 它們共用一個Engine來處理所有Connector收到的請求 -->
<Service name="Tomcat-Standalone">
<!-- Coyote HTTP/1.1 Connector className : 該Connector的實現類是org.apache.coyote.tomcat4.CoyoteConnector ***port : 在端口號8080處偵聽來自客戶browser的HTTP1.1請求*** minProcessors : 該Connector先創建5個線程等待客戶請求,每個請求由一個線程負責 maxProcessors : 當現有的線程不夠服務客戶請求時,若線程總數不足75個,則創建新線程來處理請求 acceptCount : 當現有線程已經達到最大數75時,為客戶請求排隊 當隊列中請求數超過100時,后來的請求返回Connection refused錯誤 redirectport : 當客戶請求是https時,把該請求轉發到端口8443去 其它屬性略 -->
<Connector className="org.apache.coyote.tomcat4.CoyoteConnector" port="8080" minProcessors="5" maxProcessors="75" acceptCount="100" enableLookups="true" redirectPort="8443" debug="0" connectionTimeout="20000" useURIValidationHack="false" disableUploadTimeout="true" />
<!-- Engine用來處理Connector收到的Http請求 它將匹配請求和自己的虛擬主機,並把請求轉交給對應的Host來處理 默認虛擬主機是localhost -->
<Engine name="Standalone" defaultHost="localhost" debug="0">
<!-- 日志類,目前沒有看到,略去先 -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- Realm,目前沒有看到,略去先 -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" .../>
<!-- 虛擬主機localhost appBase : 該虛擬主機的根目錄是webapps/ 它將匹配請求和自己的Context的路徑,並把請求轉交給對應的Context來處理 -->
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true">
<!-- 日志類,目前沒有看到,略去先 -->
<Logger className="org.apache.catalina.logger.FileLogger" .../>
<!-- Context,對應於一個Web App path : 該Context的路徑名是"",故該Context是該Host的默認Context docBase : 該Context的根目錄是webapps/mycontext/ -->
<Context path="" docBase="mycontext" debug="0"/>
<!-- 另外一個Context,路徑名是/wsota -->
<Context path="/wsota" docBase="wsotaProject" debug="0"/>
</Host>
</Engine>
</Service>
</Server>
<!----------------------------------------------------------------------------------------------->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
2Tomcat Server的組成說明
- Server
A Server element represents the entire Catalina servlet container. (Singleton) - Service
A Service element represents the combination of one or more Connector components that share a single Engine
Service是這樣一個集合:它由一個或者多個Connector組成,以及一個Engine,負責處理所有Connector所獲得的客戶請求 - Connector
一個Connector將在某個指定端口上偵聽客戶請求,並將獲得的請求交給Engine來處理,從Engine處獲得回應並返回客戶
TOMCAT有兩個典型的Connector,一個直接偵聽來自browser的http請求,一個偵聽來自其它WebServer的請求
Coyote Http/1.1 Connector 在端口8080處偵聽來自客戶browser的http請求,Coyote JK2 Connector 在端口8009處偵聽來自其它WebServer(Apache)的servlet/jsp代理請求 - Engine
The Engine element represents the entire request processing machinery associated with a particular Service
It receives and processes all requests from one or more Connectors and returns the completed response to the Connector for ultimate transmission back to the client Engine下可以配置多個虛擬主機Virtual Host,每個虛擬主機都有一個域名,當Engine獲得一個請求時,它把該請求匹配到某個Host上,然后把該請求交給該Host來處理,Engine有一個默認虛擬主機,當請求無法匹配到任何一個Host上的時候,將交給該默認Host來處理 - Host
代表一個Virtual Host,虛擬主機,每個虛擬主機和某個網絡域名Domain Name相匹配,每個虛擬主機下都可以部署(deploy)一個或者多個Web App,每個Web App對應於一個Context,有一個Context path,當Host獲得一個請求時,將把該請求匹配到某個Context上,然后把該請求交給該Context來處理,匹配的方法是“最長匹配”,所以一個path==”“的Context將成為該Host的默認Context,所有無法和其它Context的路徑名匹配的請求都將最終和該默認Context匹配 - Context
一個Context對應於一個Web Application,一個Web Application由一個或者多個Servlet組成Context在創建的時候將根據配置文件CATALINAHOME/conf/web.xml和 WEBAPP_HOME/WEB-INF/web.xml載入Servlet類當Context獲得請求時,將在自己的映射表(mapping table)中尋找相匹配的Servlet類,如果找到,則執行該類,獲得請求的回應,並返回。
3Tomcat Server的結構圖
4結合圖,我們研究下Tomcat Server處理一個http請求的過程
假設來自客戶的請求為:
http://localhost:8080/wsota/wsota_index.jsp
1. 請求被發送到本機端口8080,被在那里偵聽的Coyote HTTP/1.1 Connector獲得
2. Connector把該請求交給它所在的Service的Engine來處理,並等待來自Engine的回應
3. Engine獲得請求localhost/wsota/wsota_index.jsp,匹配它所擁有的所有虛擬主機Host
4. Engine匹配到名為localhost的Host(即使匹配不到也把請求交給該Host處理,因為該Host被定義為該Engine的默認主機)
5. localhost Host獲得請求/wsota/wsota_index.jsp,匹配它所擁有的所有Context
6. Host匹配到路徑為/wsota的Context(如果匹配不到就把該請求交給路徑名為”“的Context去處理)
7. path=”/wsota”的Context獲得請求/wsota_index.jsp,在它的mapping table中尋找對應的servlet
8. Context匹配到URL PATTERN為*.jsp的servlet,對應於JspServlet類
9. 構造HttpServletRequest對象和HttpServletResponse對象,作為參數調用JspServlet的doGet或doPost方法
10. Context把執行完了之后的HttpServletResponse對象返回給Host
11. Host把HttpServletResponse對象返回給Engine
12. Engine把HttpServletResponse對象返回給Connector
13. Connector把HttpServletResponse對象返回給客戶browser
4Context的部署配置文件web.xml的說明
<!----------------------------------------------------------------------------------------------->
<web-app>
<!-- 概述: 該文件是所有的WEB APP共用的部署配置文件, 每當一個WEB APP被DEPLOY,該文件都將先被處理,然后才是WEB APP自己的/WEB-INF/web.xml -->
<!-- +-------------------------+ -->
<!-- | servlet類定義部分 | -->
<!-- +-------------------------+ -->
<!-- DefaultServlet 當用戶的HTTP請求無法匹配任何一個servlet的時候,該servlet被執行 URL PATTERN MAPPING : / -->
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- InvokerServlet 處理一個WEB APP中的匿名servlet 當一個servlet被編寫並編譯放入/WEB-INF/classes/中,卻沒有在/WEB-INF/web.xml中定義的時候 該servlet被調用,把匿名servlet映射成/servlet/ClassName的形式 URL PATTERN MAPPING : /servlet/* -->
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- JspServlet 當請求的是一個JSP頁面的時候(*.jsp)該servlet被調用 它是一個JSP編譯器,將請求的JSP頁面編譯成為servlet再執行 URL PATTERN MAPPING : *.jsp -->
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>logVerbosityLevel</param-name>
<param-value>WARNING</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<!-- +---------------------------+ -->
<!-- | servlet映射定義部分 | -->
<!-- +---------------------------+ -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<!-- +------------------------+ -->
<!-- | 其它部分,略去先 | -->
<!-- +------------------------+ -->
... ... ... ...
</web-app>
<!----------------------------------------------------------------------------------------------->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
文章參考
docs.huihoo.com/apache/tomcat/heavyz/01-startup.html
www.2cto.com/os/201402/278532.html
<div class="pinfortext">
<a href="http://blog.csdn.net/Ezitai/article/details/50520665" target="_blank">查看原文>></a>
<span>
<a href="javascript:void(0)" id="click_logo" title="喜歡">
<span><i class="fa fa-heart-o" id="support_logo"></i></span>
<em>2</em>
</a>
</span>
</div>
<div class="divcontent bordertop">
<span class="title">看過本文的人也看了:</span>
<ul class="clearfix">
<li>
<a class="yellow" href="http://lib.csdn.net/base/computernetworks/structure" target="_blank" title="計算機網絡知識結構圖">
<i class="cirle"></i>
計算機網絡知識結構圖
</a>
</li>
<li>
<div class="csdn-tracking-statistics" data-mod="popu_248_lib_37">
<a basename="computernetworks" knid="1022" contentid="10965" href="http://lib.csdn.net/article/computernetworks/10965" target="_blank" title="(java 基礎知識) Java 安全套接字--javax.net.ssl">
<i class="cirle"></i>
(java 基礎知識) Java 安全套接字--ja...
</a>
</div>
</li>
<li>
<div class="csdn-tracking-statistics" data-mod="popu_248_lib_37">
<a basename="computernetworks" knid="1022" contentid="13919" href="http://lib.csdn.net/article/computernetworks/13919" target="_blank" title="Raw Socket(原始套接字)實現Sniffer(嗅探)">
<i class="cirle"></i>
Raw Socket(原始套接字)實現Sniffer(...
</a>
</div>
</li>
<li>
<div class="csdn-tracking-statistics" data-mod="popu_248_lib_37">
<a basename="computernetworks" knid="1022" contentid="13924" href="http://lib.csdn.net/article/computernetworks/13924" target="_blank" title="Linux 套接字與文件系統的關系--------套接字文件系統">
<i class="cirle"></i>
Linux 套接字與文件系統的關系------...
</a>
</div>
</li>
<li>
<div class="csdn-tracking-statistics" data-mod="popu_248_lib_37">
<a basename="computernetworks" knid="1022" contentid="38767" href="http://lib.csdn.net/article/computernetworks/38767" target="_blank" title="unix進程通信方式總結(下)">
<i class="cirle"></i>
unix進程通信方式總結(下)
</a>
</div>
</li>
<li>
<div class="csdn-tracking-statistics" data-mod="popu_248_lib_37">
<a basename="computernetworks" knid="1022" contentid="21899" href="http://lib.csdn.net/article/computernetworks/21899" target="_blank" title="第七章、網絡安全與主機基本防護: 限制端口, 網絡升級與 SELinux">
<i class="cirle"></i>
第七章、網絡安全與主機基本防護: 限...
</a>
</div>
</li>
</ul>
<div class="clearfix">
</div>
</div>
</div>