apache与tomcat互联配置

首先我们先介绍一下为什么要让 Apache 与 Tomcat 之间进行连接。事实上 Tomcat 本身已经提供了 HTTP 服务,该服务默认的端口是 8080,装好 tomcat 后通过 8080 端口可以直接使用 Tomcat 所运行的应用程序,你也可以将该端口改为 80。
 
既然 Tomcat 本身已经可以提供这样的服务,我们为什么还要引入 Apache 或者其他的一些专门的 HTTP 服务器呢?原因有下面几个:
1.提升对静态文件的处理性能
2.利用 Web 服务器来做负载均衡以及容错
3.无缝的升级应用程序
4.重写静态化的URL,为了SEO
 
这四点对一个 web 网站来说是非常之重要的,我们希望我们的网站不仅是速度快,而且要稳定,不能因为某个 Tomcat 宕机或者是升级程序导致用户访问不了,而能完成这几个功能的、最好的 HTTP 服务器也就只有 apache 的 http server 了,它跟 tomcat 的结合是最紧密和可靠的。
 
接下来介绍如何互联:
 

apache配置:

httpd.conf新增&修改配置:
#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule’ lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l’) do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
LoadModule    jk_module  modules/mod_jk.so      
Include /home/work/beidou-mgr/apache/conf/mod_jk.conf
 
 
NameVirtualHost *:8088
<VirtualHost *:8088>
#    ServerName wm123.baidu.com
#    ServerAlias zhan.baidu.com zhandian.baidu.com
    DocumentRoot /home/work/zhangxu/wm123-web
    ErrorLog "|/home/work/beidou/apache/bin/cronolog /home/work/beidou/apache/logs/%Y%m%d/wm123.error_log"
    CustomLog "|/home/work/beidou/apache/bin/cronolog /home/work/beidou/apache/logs/%Y%m%d/wm123.access_log.%H" combined
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/site/([0-9]*)\.html.*/site/detail.action?siteId=1 [L,PT,NC]
RewriteRule ^/index.htm.*$ /index/index.action [L,PT,NC]
RewriteRule ^/$ /index/index.action [L,PT,NC]
</IfModule>
    JkMount /* loadbalancer_wm123
    JkUnMount /*.css loadbalancer_wm123
    JkUnMount /*.img loadbalancer_wm123
    JkUnMount /*.html loadbalancer_wm123
    JkUnMount /*.gif loadbalancer_wm123
    JkUnMount /*.jpg loadbalancer_wm123
    JkUnMount /*.swf loadbalancer_wm123
    JkUnMount /*.bmp loadbalancer_wm123
    JkUnMount /*.csv loadbalancer_wm123
    JkUnMount /*.png loadbalancer_wm123
    JkUnMount /*.js loadbalancer_wm123
</VirtualHost>
 
 
mod_jk.conf配置
<IfModule mod_jk.c>
        JkWorkersFile /home/work/beidou/apache/conf/workers.properties
        JkLogFile "| /home/work/beidou/apache/bin/cronolog  /home/work/beidou/apache/logs/%Y%m%d/mod_jk.log"
        JkLogLevel warn
        JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
        JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
        JkRequestLogFormat "%w %V %R %T %B %U"
 
        JkShmFile "/home/work/beidou/apache/logs/shm.file"
        JkAutoAlias "/home/work/beidou/apache/htdocs"
        JkLogLevel info
 
        # Sample JkMounts.  Replace these with the paths you would
        # like to mount from your JSP server.
          JkMount /*.jsp ajp13w
          JkMount /*.action ajp13w
          JkMount /*.gif ajp13w
          JkMount /*.jpg ajp13w
          JkMount /*.css ajp13w
          JkMount /servlet/* ajp13w
          JkMount /examples/* ajp13w
          JkMount /*.js ajp13w
          JkMount /*.html ajp13w
          JkMount /*.bmp ajp13w
          JkMount /*.csv ajp13w
          JkMount /*.dwr ajp13w
</IfModule>
 
workers.properties配置:
worker.list=worker3,jkstatus,loadbalancer_wm123
 
worker.worker3.type=ajp13
worker.worker3.host=localhost
worker.worker3.port=8228                     # 这个是tomcat中配置的AJP端口!
worker.worker3.connection_pool_size=128
worker.worker3.connection_pool_timeout=90
worker.worker3.socket_keepalive=1
worker.worker3.socket_timeout=300
 
worker.loadbalancer_wm123.type=lb
worker.loadbalancer_wm123.balance_workers=worker3
worker.jkstatus.type=status
 
 
 
AJP是定向包协议。这是最常见的方式,JK 本身有两个版本分别是1和2,目前1常用的是1.3,而版本2早已经废弃了,以后不再有新版本的推出了,所以建议你采用版本 1。
JK是通过AJP协议与Tomcat服务器进行通讯的,Tomcat默认的 AJP Connector的端口是 8009。
 

tomcat配置:

server.xml配置:
   <!– Define an AJP 1.3 Connector on port 8009 –>
    <!–Connector port="8009" 
               enableLookups="false" redirectPort="8443" protocol="AJP/1.3" /–>

Leave a Comment.