首页 > 代码库 > apache反向代理后端tomcat
apache反向代理后端tomcat
在各种企业级网站应用系统中,JSP也是使用较多的一种网站开发语言,对于这样的网站服务器,必须能够支持Java环境、JSP解析,比如安装Tomcat、Jboss等JSP容器。另外一方面,Apache HTTP Server对静态HTML页面的处理能力要更加优秀一些,因此若能将Apache与Tomcat等JSP容器整合在一起使用,将可以获得更好的网站服务体验。
这种情况下,Apache仍然作为浏览器访问的前端服务,当客户机请求访问*.jsp网页时,再自动调用后端的tomcat服务进行解析,并将结果页面返回给客户机。其间的沟通过程需要安装连接器mod_jk来完成。
后端安装Tomcat:
安装jdk开发环境 [root@localhost ~]# cd /setup/ [root@www setup]# chmod +x jdk-6u20-linux-i586.bin [root@www setup]# ./jdk-6u20-linux-i586.bin 解压tomcat [root@localhost setup]# vim /var/www/html/index.jsp tar zxf apache-tomcat-7.0.23.tar.gz -C /usr/local/tomcat [root@localhost ~]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/sbin/tomcat-up [root@localhost ~]# ln -s /usr/local/tomcat/bin/shudown.sh /usr/local/sbin/tomcat-down 添加环境变量vim /etc/profile: JAVA_HOME=/setup/jdk1.6.0_20 CATALINA_HOME=/usr/local/tomcat CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/servlet-api.jar PATH=$JAVA_HOME/bin:$PATH export JAVA_HOME CLASSPATH PATH CATALINA_HOME 测试jdk: [root@localhost setup]# java -version java version "1.6.0_20" Java(TM) SE Runtime Environment (build 1.6.0_20-b02) Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing) 修改tomcat的server.xml配置文件: [root@localhost setup]# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.w4ya.com" jvmRoute="jvm1"> #把默认主机改为"www.w4ya.com" <Host name="www.w4ya.com" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/var/www/html/" debug="0" /> //新建虚拟主机 </Host> 启动tomcat服务,并确认tomcat服务的监听状态。 [root@localhost ~]# tomcat-up Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.6.0_20 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@localhost ~]# netstat -anpt | grep java tcp 0 0 :::8009 :::* LISTEN 2591/java tcp 0 0 :::8080 :::* LISTEN 2591/java
在前端服务器安装apache
[root@localhost http]# tar xf apr-1.5.1.tar.gz [root@localhost http]# cd apr-1.5.1 [root@localhost apr-1.5.1]# ./configure --prefix=/usr/local/apr [root@localhost apr-1.5.1]# make && make install [root@localhost http]# tar xf apr-util-1.5.4.tar.gz [root@localhost http]# cd apr-util-1.5.4 [root@localhost apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@localhost apr-util-1.5.4]# make && make install with-apr=/usr/local/apr [root@localhost setup]# tar jxf pcre-8.35.tar.bz2 [root@localhost setup]# cd pcre-8.35 [root@localhost pcre-8.35]# ./configure [root@localhost pcre-8.35]# make && make install [root@localhost httpd-2.4.10]# ./configure --prefix=/usr/local/apache --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-mpms-shared=all --with-mpm=event --enable-proxy --enable-proxy-http --enable-proxy-ajp --enable-proxy-balancer --enable-lbmethod-heartbeat --enable-hearbeat --enable-slotmem-shm --enable-slotmem-plain --enable-watchdog [root@localhost httpd-2.4.10]# make && make install [root@localhost conf]# vim /usr/local/apache/conf/httpd.conf LoadModule slotmem_shm_module modules/mod_slotmem_shm.so //打开这个模块
第一种方法apache直接代理tomcat:
[root@localhost conf]# vim /usr/local/apache/conf/httpd.conf //引入配置文件 include /usr/local/apache/conf/http-proxy.conf [root@localhost conf]# vim http-proxy.conf //在配置文件添加以下内容 <VirtualHost *:80> ProxyVia On ProxyRequests Off ProxyPreserveHost On ProxyPass / http://192.168.1.115:8080 ProxyPassReverse / http://192.168.1.115:8080 <Proxy *> Require all granted </Proxy> <Location / > Require all granted </Location> </VirtualHost>
第二种方法使用mod_jk代理:
[root@localhost setup]# weget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz [root@localhost setup]# tar xf tomcat-connectors-1.2.40-src.tar.gz [root@localhost setup]# cd tomcat-connectors-1.2.40-src/native/ [root@localhost native]# ./configure --with-apxs=/usr/local/apache/bin/apxs [root@localhost native]#make && make install [root@localhost modules]# ls /usr/local/apache/modules/ |grep "mod_jk.so" mod_jk.so [root@localhost conf]# vim /usr/local/apache/conf/httpd.conf //添加以下内容: LoadModule jk_module modules/mod_jk.so JkWorkersFile /usr/local/apache/conf/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkShmFile logs/jk.shm JkMount /* Tomcat JkMount /status/ stat [root@localhost conf]# vim workers.properties //容器配置文件 worker.list=Tomcat,stat1 worker.Tomcat.port=8009 worker.Tomcat.host=192.168.1.116 worker.Tomcat.type=ajp13 worker.Tomcat.lbfactor=1 worker.stat.type = status 重启apache
apache代理到tomcatB:
本文转自:维斯亚综合论坛:http://www.w4ya.com/forum.php
本文出自 “龙爱雪琪” 博客,转载请与作者联系!
apache反向代理后端tomcat
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。