本文內容轉載整理自《Apache HTTP Server 與 Tomcat 的三種鏈接方式介紹》(http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/)css
Apache與Tomcat整合的優點有如下幾點:html
1. 提高對靜態文件的處理性能shell
2. 利用 Web 服務器來作負載均衡以及容錯apache
3. 無縫的升級應用程序tomcat
整合方式一:使用Apache的mod_jk.so模塊服務器
使用該方式須要三個配置文件,分別是httpd.conf、workers.properties、uriworkermap.properties,這三個配置文件均需放在apache的conf目錄下。session
示例內容以下:負載均衡
httpd.conf性能
# Add module mod_jk.so LoadModule jk_module modules/mod_jk.so # # Configure mod_jk # # Configure worker JkWorkersFile conf/workers.properties # Configure uri JkMountFile conf/uriworkermap.properties JkLogFile logs/mod_jk.log JkLogLevel warn
workers.propertiesspa
# # workers.properties # # list the workers by name # DLOG4J is worker worker.list=DLOG4J, status # localhost server 1 # ------------------------ worker.s1.port=8009 worker.s1.host=localhost worker.s1.type=ajp13 # localhost server 2 # ------------------------ worker.s2.port=8519 worker.s2.host=192.168.200.14 worker.s2.type=ajp13 worker.s2.stopped=1 # configure the type of DLOG4J is loadbalance worker.DLOG4J.type=lb worker.retries=3 # configure s1 and s2 is balanced workers worker.DLOG4J.balanced_workers=s1, s2 worker.DLOG4J.sticky_session=1 worker.status.type=status
uriworkermap.properties
# # uriworkermap.properties # /*=DLOG4J /jkstatus=status !/*.gif=DLOG4J !/*.jpg=DLOG4J !/*.png=DLOG4J !/*.css=DLOG4J !/*.js=DLOG4J !/*.htm=DLOG4J !/*.html=DLOG4J
整合方式二:使用Apache的mod_proxy.so模塊
http_proxy
注意:使用Apache的http_proxy,Tomcat必須配置HTTP Connector使其能支持HTTP協議。
單機時,可用如下配置
ProxyPass /images ! ProxyPass /css ! ProxyPass /js ! ProxyPass / http://localhost:8080/
機器時,可用如下配置
ProxyPass /images ! ProxyPass /css ! ProxyPass /js ! ProxyPass / balancer://example/ <Proxy balancer://example/> BalancerMember http://server1:8080/ BalancerMember http://server2:8080/ BalancerMember http://server3:8080/ </Proxy>
ajp_proxy
注意:使用Apache的ajp_proxy,Tomcat必須配置AJP Connector使其能支持AJP協議。
ProxyPass /images ! ProxyPass /css ! ProxyPass /js ! ProxyPass / balancer://example/ <Proxy balancer://example/> BalancerMember ajp://server1:8080/ BalancerMember ajp://server2:8080/ BalancerMember ajp://server3:8080/ </Proxy>