詳解Tomcat HTTP協(xié)議與AJP協(xié)議
Tomcat最主要的功能是提供Servlet/JSP容器,盡管它也可以作為獨(dú)立的Java Web服務(wù)器,它在對靜態(tài)資源(如HTML文件或圖像文件)的處理速度,以及提供的Web服務(wù)器管理功能方面都不如其他專業(yè)的HTTP服務(wù)器,如IIS和Apache服務(wù)器。
因此在實(shí)際應(yīng)用中,常常把Tomcat與其他HTTP服務(wù)器集成。對于不支持Servlet/JSP的HTTP服務(wù)器,可以通過Tomcat服務(wù)器來運(yùn)行Servlet/JSP組件。
當(dāng)Tomcat與其他HTTP服務(wù)器集成時(shí),Tomcat服務(wù)器的工作模式通常為進(jìn)程外的Servlet容器,Tomcat服務(wù)器與其他HTTP服務(wù)器之間通過專門的插件來通信。
Tomcat在server.xml中配置了兩種連接器。
相關(guān)概念
Web客戶訪問Tomcat服務(wù)器的兩種方式
- HTTP Connector:擁有這個(gè)連接器,Tomcat才能成為一個(gè)web服務(wù)器,但還可處理Servlet和jsp。
- AJP Connector:AJP連接器可以通過AJP協(xié)議和另一個(gè)web容器進(jìn)行交互。
Connector的配置
對Connector的配置位于conf/server.xml文件中。
1. BIO HTTP/1.1 Connector配置
一個(gè)典型的配置如下:
- <!--
- Code highlighting produced by Actipro CodeHighlighter (freeware)
- http://www.CodeHighlighter.com/
- --> connectionTimeout=”20000” redirectPort=”8443”
其它一些重要屬性如下:
- acceptCount : 接受連接request的最大連接數(shù)目,默認(rèn)值是10
- address : 綁定IP地址,如果不綁定,默認(rèn)將綁定任何IP地址
- allowTrace : 如果是true,將允許TRACE HTTP方法
- compressibleMimeTypes : 各個(gè)mimeType, 以逗號分隔,如text/html,text/xml
- compression : 如果帶寬有限的話,可以用GZIP壓縮
- connectionTimeout : 超時(shí)時(shí)間,默認(rèn)為60000ms (60s)
- maxKeepAliveRequest : 默認(rèn)值是100
- maxThreads : 處理請求的Connector的線程數(shù)目,默認(rèn)值為200
如果是SSL配置,如下:
- <!--
- Code highlighting produced by Actipro CodeHighlighter (freeware)
- http://www.CodeHighlighter.com/
- -->maxThreads="150" scheme="https" secure="true"
- clientAuth="false" sslProtocol = "TLS"
- address="0.0.0.0"
- keystoreFile="E:/java/jonas-full-5.1.0-RC3/conf/keystore.jks"
- keystorePass="changeit" />
其中,keystoreFile為證書位置,keystorePass為證書密碼
2. NIO HTTP/1.1 Connector配置
- <!--
- Code highlighting produced by Actipro CodeHighlighter (freeware)
- http://www.CodeHighlighter.com/
- --><Connector port=”8080” protocol=”org.apache.coyote.http11.Http11NioProtocol”
- maxThreads=”150” connectionTimeout=”20000” redirectPort=”8443”
3. Native APR Connector配置
ARP是用C/C++寫的,對靜態(tài)資源(HTML,圖片等)進(jìn)行了優(yōu)化。所以要下載本地庫
tcnative-1.dll與openssl.exe,將其放在%tomcat%\bin目錄下。
在server.xml中要配置一個(gè)Listener:
- <!--
- Code highlighting produced by Actipro CodeHighlighter (freeware)
- http://www.CodeHighlighter.com/
- --><!--APR library loader. Documentation at /docs/apr.html -->
- <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
- <!-- 配置使用APR connector --》
- <Connector port=”8080” protocol=”org.apache.coyote.http11.Http11AprProtocol”
- maxThreads=”150” connectionTimeout=”20000” redirectPort=”8443”