茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1730254 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_0043.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2017/3/3 7:48:09
apache使用mod_proxy反向代理tomcat
2016-12-28 09:01:54
標籤:apache tomcat mod
原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://10927734.blog.51cto.com/10917734/1886752

說明:所使用的操作系統為Centos6.3,準備三台虛擬機,202.207.178.8(做為前端安裝apache,作為代理),node1:202.207.178.6(已經安裝了Tomcat,並可以訪問到其默認頁面,作為後端),node2:202.207.178.7(已經安裝了Tomcat,並可以訪問到其默認頁面,作為後端)

Tomcat相關安裝配置詳見本人博客:http://10927734.blog.51cto.com/10917734/1874112

一、配置後端Tomcat(兩台配置基本相同,為了以示區別,將兩者顯示可設為不同)
1、編輯配置文件,自定義Host,並檢查有無語法錯誤
		# cd /usr/local/tomcat/conf/
		# vim server.xml
		#將引擎的默認主機改為自己定義的主機
			<Engine name="Catalina" defaultHost="www.fsy.com"
              jvmRoute="TomcatA">
		#自定義一個Host
			<Host name="www.fsy.com"  appBase="/web"
				unpackWARs="true" autoDeploy="true">
			 <Context path="" docBase="webapps" reLoadable="true" />
			</Host>
		# catalina.sh configtest

2、提供訪問頁面
		# mkdir -pv /web/webapps
		# cd /web/webapps/
		# vim index.jsp

添加以下內容:
			<%@ page language="java" %>
			<html>
			 <head><title>TomcatA</title></head>
			 <body>
				<h1><font color="red">TomcatA </font></h1>
				<table align="centre" border="1">
				 <tr>
					<td>Session ID</td>
				<% session.setAttribute("abc","abc"); %>
					<td><%= session.getId() %></td>
				 </tr>
				 <tr>
					<td>Created on</td>
					<td><%= session.getCreationTime() %></td>
				</tr>
				</table>
			 </body>
			</html>

3、啟動Tomcat服務,即可進行訪問測試了
		# service tomcat start

http://202.207.178.7:8080/

二、配置前端Apache
1、解決依賴關係
httpd-2.4.4需要較新版本的apr和apr-util,因此需要事先對其進行升級。這裡使用
源碼包進行升級(apr-1.5.2,apr-util-1.5.4 )
(1) 編譯安裝apr
			# tar xf apr-1.5.2.tar.bz2
			# cd apr-1.5.2
			# ./configure --prefix=/usr/local/apr
			# make && make install

(2) 編譯安裝apr-util
			# tar xf apr-util-1.5.4.tar.bz2
			# cd apr-util-1.5.4
			# ./configure --prefix=/usr/local/apr-util
             --with-apr=/usr/local/apr
			# make && make install

(3)httpd-2.4.4編譯過程也要依賴於pcre-devel軟件包,需要事先安裝。
			#yum -y install pcre-devel

(4)可在編譯安裝httpd時會報錯:checking for OpenSSL version >=0.9.7 ...
			#yum -y install openssl-devel
			#yum update openssl

2、如果發現以前使用rpm包裝過httpd的解決辦法(重新安裝一次即可)
		# rpm -e httpd --nodeps
		# rm -rf /etc/httpd/
		# make install

3、編譯安裝httpd-2.4.4
		# tar xf httpd-2.4.4.tar.bz2
		# cd httpd-2.4.4
		#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --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-heartbeat --enable-slotmem-shm
         --enable-slotmem-plain --enable-watchdog
		# make && make install

4、提供SysV服務腳本/etc/rc.d/init.d/httpd,內容如下:
		#!/bin/bash
		#
		# httpd        Startup script for the Apache HTTP Server
		#
		# chkconfig: - 85 15
		# description: Apache is a World Wide Web server.  It is used to serve \
		#	      HTML files and CGI.
		# processname: httpd
		# config: /etc/httpd/conf/httpd.conf
		# config: /etc/sysconfig/httpd
		# pidfile: /var/run/httpd.pid

		# Source function library.
		. /etc/rc.d/init.d/functions

		if [ -f /etc/sysconfig/httpd ]; then
				. /etc/sysconfig/httpd
		fi

		# Start httpd in the C locale by default.
		HTTPD_LANG=${HTTPD_LANG-"C"}

		# This will prevent initlog from swallowing up a pass-phrase prompt if
		# mod_ssl needs a pass-phrase from the user.
		INITLOG_ARGS=""

		# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
		# with the thread-based "worker" MPM; BE WARNED that some modules may not
		# work correctly with a thread-based MPM; notably PHP will refuse to start.

		# Path to the apachectl script, server binary, and short-form for messages.
		apachectl=/usr/local/apache/bin/apachectl
		httpd=${HTTPD-/usr/local/apache/bin/httpd}
		prog=httpd
		pidfile=${PIDFILE-/var/run/httpd.pid}
		lockfile=${LOCKFILE-/var/lock/subsys/httpd}
		RETVAL=0

		start() {
				echo -n $"Starting $prog: "
				LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd                 $OPTIONS
				RETVAL=$?
				echo
				[ $RETVAL = 0 ] && touch ${lockfile}
				return $RETVAL
		}
		stop() {
			echo -n $"Stopping $prog: "
			killproc -p ${pidfile} -d 10 $httpd
			RETVAL=$?
			echo
			[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
		}
		reload() {
			echo -n $"Reloading $prog: "
			if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
				RETVAL=$?
				echo $"not reloading due to configuration syntax error"
				failure $"not reloading $httpd due to configuration syntax                 error"
			else
				killproc -p ${pidfile} $httpd -HUP
				RETVAL=$?
			fi
			echo
		}

		# See how we were called.
		case "$1" in
		 start)
			start
			;;
		 stop)
			stop
			;;
		 status)
				status -p ${pidfile} $httpd
			RETVAL=$?
			;;
		 restart)
			stop
			start
			;;
		 condrestart)
			if [ -f ${pidfile} ] ; then
				stop
				start
			fi
			;;
		 reload)
				reload
			;;
		 graceful|help|configtest|fullstatus)
			$apachectl $@
			RETVAL=$?
			;;
		 *)
			echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
			exit 1
		esac

		exit $RETVAL

5、而後為此腳本賦予執行權限並加入服務列表,便可啟動服務了!:
		# chmod +x /etc/rc.d/init.d/httpd
		# chkconfig --add httpd
		# service httpd start

6、配置apache通過mod_proxy模塊與Tomcat連接(代理至202.207.178.7)
要使用mod_proxy與Tomcat實例連接,需要apache已經裝載mod_proxy、mod_proxy_http、mod_proxy_ajp和proxy_balancer_module
(實現Tomcat集群時用到)等模塊
(1)查看上述模塊是否已經裝載:
			# /usr/local/apache/bin/httpd -D DUMP_MODULES | grep  proxy
			proxy_module (shared)
			proxy_connect_module (shared)
			proxy_ftp_module (shared)
			proxy_http_module (shared)
			proxy_fcgi_module (shared)
			proxy_scgi_module (shared)
			proxy_ajp_module (shared)
			proxy_balancer_module (shared)
			proxy_express_module (shared)

(2)修改主配置文件
			# vim /etc/httpd/httpd.conf
				註釋中心主機:
					#DocumentRoot "/usr/local/apache/htdocs"
				添加虛擬主機相關的內容:
					Include /etc/httpd/extra/httpd-proxy.conf
				添加pid文件:
					PidFile "/var/run/httpd.pid"

(3)添加從配置文件:
			# vim /etc/httpd/extra/httpd-proxy.conf
				<VirtualHost *:80>
					ProxyVia On
					ProxyRequests Off
					ProxyPass / http://202.207.178.7:8080/
					ProxyPassReverse / http://202.207.178.7:8080/
					<Proxy *>
							Require all granted
					</Proxy>
					<Location  / >
							Require all granted
					</Location>
				</VirtualHost>

(4)啟動httpd服務
			# service httpd restart

啟動報錯,查看/usr/local/apache/logs/error_log發現報錯,需要啟動兩個模塊
			# vim /etc/httpd/httpd.conf
	    LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
		  LoadModule socache_shmcb_modulemodules/mod_socache_shmcb.so

訪問http://202.207.178.8/,可以訪問到後端!

7、配置apache通過mod_proxy模塊與Tomcat連接,實現負載均衡
(1)修改從配置文件,改為以下內容
			# vim /etc/httpd/extra/httpd-proxy.conf
				ProxyRequests Off
				<proxy balancer://lbcluster1>
			BalancerMember ajp://202.207.178.6:8009 loadfactor=10 route=TomcatA
			BalancerMember ajp://202.207.178.7:8009 loadfactor=10 route=TomcatB
			ProxySet lbmethod=byrequests
				</proxy>
				<VirtualHost *:80>
						#沒啥用,可以不定義
						ServerName localhost
						ProxyVia On
						ProxyPass / balancer://lbcluster1/
						ProxyPassReverse / balancer://lbcluster1/
						<Proxy *>
								Require all granted
						</Proxy>
						<Location  / >
								Require all granted
						</Location>
				</VirtualHost>

訪問http://202.207.178.8/,可以訪問到後端,並實現負載均衡!

8、提供後端狀態頁面
(1)修改配置文件,在虛擬主機中添加以下內容
			# vim /etc/httpd/extra/httpd-proxy.conf
			       <Location /balancer-manager>
					SetHandler balancer-manager
					Proxypass !
					Require all granted
					</Location>

(2)重啟服務,即可訪問測試了
http://202.207.178.8/balancer-manager,可以訪問到一個狀態頁面

原文出處:apache使用mod_proxy反向代理tomcat - 10917734 - 51CTO技术博客
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|