您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
Tomcat系列之apache使用mod_jk和mod_proxy反向代理tomcat
发布时间:2019-07-20 01:52:30编辑:雪饮阅读()
集群架构与环境:
redhat6.4-i386用httpd-2.4.4做前端
两台redhat5.8-i386用apache-tomcat-7.0.33做后端
前端提供对外访问的网卡接口
后端与前端通过仅主机网卡通信
ip规划:
前端对外:192.168.2.172
前端对内:192.168.128.134
后端1:192.168.128.135
后端2:192.168.128.136
前端环境搭建
[root@localhost src]# tar -xvf apr-1.4.6.tar.bz2
[root@localhost apr-1.4.6]# ./configure --prefix=/usr/local/apr --disable-ipv6
[root@localhost apr-1.4.6]# make
[root@localhost apr-1.4.6]# make install
[root@localhost src]# tar xvf apr-util-1.5.2.tar.bz2
[root@localhost apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.5.2]# make
[root@localhost apr-util-1.5.2]# make install
[root@localhost src]# tar -xvf httpd-2.4.4.tar.bz2
[root@localhost httpd-2.4.4]# ./configure --prefix=/usr/local/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
[root@localhost httpd-2.4.4]#make
[root@localhost httpd-2.4.4]#make install
后端环境搭建
jdk环境配置见:Tomcat系列之java技术详解
安装tomcat见:Tomcat系列之组件详解及服务安装配置
mod_proxy实现前端httpd为后端tomcat反向代理
先以为其中一个后端反向代理为例
后端1配置
[root@localhost conf]# cat /usr/local/tomcat/conf/server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="www.xynes.cn" jvmRoute="TomcatA">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.xynes.cn" appBase="/web"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="webapps" reLoadable="true" />
</Host>
</Engine>
</Service>
</Server>
[root@localhost conf]# mkdir -p /web/webapps
然后用启动tomcat服务
/usr/local/tomcat/bin/catalina.sh start
再放上一个测试用的jsp脚本
[root@localhost /]# cat /web/webapps/cs.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>
然后测试访问
前端apache配置
httpd.conf:
去除中心主机:DocumentRoot "/usr/local/httpd/htdocs"
添加httpd-proxy(虚拟主机)配置,位于“<IfModule proxy_html_module>”中
#Include conf/extra/httpd-default.conf
Include conf/extra/httpd-proxy.conf
去除该模块的注释
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
去除该处注释
ServerName www.example.com:80
则虚拟主机配置文件:
[root@localhost extra]# cat /usr/local/httpd/conf/extra/httpd-proxy.conf
<VirtualHost *:80>
ProxyVia On
ProxyRequests Off
ProxyPass / http://192.168.128.135:8080/
ProxyPassReverse / http://192.168.128.135:8080/
<Proxy *>
Require all granted
</Proxy>
<Location / >
Require all granted
</Location>
</VirtualHost>
然后启动apache并测试效果
如果能出现上面的界面,则基于mod_proxy方式的反向代理就完成了。
mod_jk实现前端httpd为后端tomcat反向代理
在mod_proxy的方式上稍作修改即可完成mod_jk实现前端httpd为后端tomcat反向代理
前端配置
为apache安装tomcat-connectors模块
[root@localhost src]# tar -zxvf tomcat-connectors-1.2.37-src.tar.gz
[root@localhost src]#cd tomcat-connectors-1.2.37-src/native/
[root@localhost native]# ./configure --with-apxs=/usr/local/httpd/bin/apxs
[root@localhost native]# make
[root@localhost native]# make install
再次配置httpd.conf
将上一方式所配置的Include conf/extra/httpd-proxy.conf注释掉并新增如下:
Include conf/extra/httpd-jk.conf
则httpd-jk.conf配置如:
[root@localhost extra]# cat /usr/local/httpd/conf/extra/httpd-jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /usr/local/httpd/conf/extra/workers.properties
JkLogFile /usr/local/httpd/logs/mod_jk.log
JkLogLevel debug
JkMount /* TomcatA
JkMount /status/ stat1
则workers.properties配置如:
[root@localhost extra]# cat /usr/local/httpd/conf/extra/workers.properties
worker.list = TomcatA,stat1
worker.TomcatA.port = 8009
worker.TomcatA.host = 192.168.128.135
worker.TomcatA.type = ajp13
worker.TomcatA.lbfactor = 1
worker.stat1.type = status
然后重启前端,再次测试反向代理,多用几个不同浏览器测试,和上面的方式效果一样就ok了
关键字词:tomcat,apache,mod_jk,mod_proxy,反向代理
相关文章
- Tomcat系列之组件详解及服务安装配置
- Nginx反向代理、负载均衡、缓存、URL重写及读写分离
- redhat5.8搭建apache2.2与vhost多虚拟主机配置
- nginx-反向代理实现nginx+apache动静分离
- linux下tomcat建立php虚拟主机整合wdcp
- apache假死崩溃的解决
- 解决linux下Apache下403 Forbidden错误(apache-2.4)
- centos配置apache2.4.23+php5.3.29+apc3.1.13
- windows+apache+php+mysql环境下配置MP4视频流拖拽插
- RedhatEnterpriseLinux4-安装apache超详细教程