1. 程式人生 > 實用技巧 >apache反向代理後端tomcat

apache反向代理後端tomcat

在各種企業級網站應用系統中,JSP也是使用較多的一種網站開發語言,對於這樣的網站伺服器,必須能夠支援Java環境、JSP解析,比如安裝Tomcat、Jboss等JSP容器。另外一方面,Apache HTTP Server對靜態HTML頁面的處理能力要更加優秀一些,因此若能將Apache與Tomcat等JSP容器整合在一起使用,將可以獲得更好的網站服務體驗。

這種情況下,Apache仍然作為瀏覽器訪問的前端服務,當客戶機請求訪問*.jsp網頁時,再自動呼叫後端的tomcat服務進行解析,並將結果頁面返回給客戶機。其間的溝通過程需要安裝聯結器mod_jk來完成。


後端安裝Tomcat:

安裝jdk開發環境
[
[email protected]
~]#cd/setup/ [[email protected]setup]#chmod+xjdk-6u20-linux-i586.bin [[email protected]setup]#./jdk-6u20-linux-i586.bin 解壓tomcat [[email protected]setup]#vim/var/www/html/index.jsptarzxfapache-tomcat-7.0.23.tar.gz-C/usr/local/tomcat [[email protected]~]#ln-s/usr/local/tomcat/bin/startup.sh/usr/local/sbin/tomcat-up [
[email protected]
~]#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 exportJAVA_HOMECLASSPATHPATHCATALINA_HOME 測試jdk: [
[email protected]
setup]#java-version javaversion"1.6.0_20" Java(TM)SERuntimeEnvironment(build1.6.0_20-b02) JavaHotSpot(TM)ClientVM(build16.3-b01,mixedmode,sharing) 修改tomcat的server.xml配置檔案: [[email protected]setup]#vim/usr/local/tomcat/conf/server.xml <Enginename="Catalina"defaultHost="www.w4ya.com"jvmRoute="jvm1">#把預設主機改為"www.w4ya.com" <Hostname="www.w4ya.com"appBase="webapps" unpackWARs="true"autoDeploy="true"> <Contextpath=""docBase="/var/www/html/"debug="0"/>//新建虛擬主機 </Host> 啟動tomcat服務,並確認tomcat服務的監聽狀態。 [[email protected]~]#tomcat-up UsingCATALINA_BASE:/usr/local/tomcat UsingCATALINA_HOME:/usr/local/tomcat UsingCATALINA_TMPDIR:/usr/local/tomcat/temp UsingJRE_HOME:/usr/local/jdk1.6.0_20 UsingCLASSPATH:/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [[email protected]~]#netstat-anpt|grepjava tcp00:::8009:::*LISTEN2591/java tcp00:::8080:::*LISTEN2591/java



在前端伺服器安裝apache

[[email protected]http]#tarxfapr-1.5.1.tar.gz
[[email protected]http]#cdapr-1.5.1
[[email protected]apr-1.5.1]#./configure--prefix=/usr/local/apr
[[email protected]apr-1.5.1]#make&&makeinstall
[[email protected]http]#tarxfapr-util-1.5.4.tar.gz
[[email protected]http]#cdapr-util-1.5.4
[[email protected]apr-util-1.5.4]#./configure--prefix=/usr/local/apr-util--with-apr=/usr/local/apr
[[email protected]apr-util-1.5.4]#make&&makeinstall
with-apr=/usr/local/apr
[[email protected]setup]#tarjxfpcre-8.35.tar.bz2
[[email protected]setup]#cdpcre-8.35
[roo[email protected]pcre-8.35]#./configure
[[email protected]pcre-8.35]#make&&makeinstall
[[email protected]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
[[email protected]httpd-2.4.10]#make&&makeinstall
[[email protected]conf]#vim/usr/local/apache/conf/httpd.conf
LoadModuleslotmem_shm_modulemodules/mod_slotmem_shm.so//開啟這個模組


第一種方法apache直接代理tomcat:

[[email protected]conf]#vim/usr/local/apache/conf/httpd.conf
//引入配置檔案
include/usr/local/apache/conf/http-proxy.conf
[[email protected]conf]#vimhttp-proxy.conf
//在配置檔案新增以下內容
<VirtualHost*:80>
ProxyViaOn
ProxyRequestsOff
ProxyPreserveHostOn
ProxyPass/http://192.168.1.115:8080
ProxyPa***everse/http://192.168.1.115:8080
<Proxy*>
Requireallgranted
</Proxy>
<Location/>
Requireallgranted
</Location>
</VirtualHost>


wKiom1SHCpPhwR36AAEFjbjgw64735.jpg



第二種方法使用mod_jk代理:

[[email protected]setup]#wegethttp://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.40-src.tar.gz
[[email protected]setup]#tarxftomcat-connectors-1.2.40-src.tar.gz
[[email protected]setup]#cdtomcat-connectors-1.2.40-src/native/
[[email protected]native]#./configure--with-apxs=/usr/local/apache/bin/apxs
[[email protected]native]#make&&makeinstall
[[email protected]modules]#ls/usr/local/apache/modules/|grep"mod_jk.so"
mod_jk.so
[[email protected]conf]#vim/usr/local/apache/conf/httpd.conf
//新增以下內容:
LoadModulejk_modulemodules/mod_jk.so
JkWorkersFile/usr/local/apache/conf/workers.properties
JkLogFilelogs/mod_jk.log
JkLogLeveldebug
JkShmFilelogs/jk.shm
JkMount/*Tomcat
JkMount/status/stat
[[email protected]conf]#vimworkers.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:

wKioL1SHDOLixMs9AAD6shgkM5g887.jpg


本文轉自:維斯亞綜合論壇:http://www.w4ya.com/forum.php

轉載於:https://blog.51cto.com/dragon123/1588105