• 欢迎访问VPS岛网站,国外VPS,国内VPS,国外服务器,国内服务器,服务器主机,测评及优惠码,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站 QQ群

Apache + Tomcat基于AJP协议实现反代、动静分离、负载均衡和session会话保持

Apache技术 Marion0728 66次浏览 已收录 0个评论

1、演示环境:

IP

操作系统

部署程序

192.168.1.143

CentOS 7.6

Apache

192.168.1.144

CentOS 7.6

Tomcat

192.168.1.145

CentOS 7.6

Tomcat

2、192.168.1.144节点创建测试页:

# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatA</title></head>

<body>

<h1><font color="red">TomcatA</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

3、192.168.1.145节点创建测试页:

# vim /usr/local/tomcat/webapps/ROOT/test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<html>

<head><title>TomcatB</title></head>

<body>

<h1><font color="blue">TomcatB</font></h1>

<table border="1">

<tr>

<td>Session ID</td>

<td><%= session.getId() %></td>

</tr>

<tr>

<td>Created ON</td>

<td><%= session.getCreationTime() %></td>

</tr>

</table>

</body>

</html>

4、192.168.1.144节点新增jvmRoute参数:

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatA">

5、192.168.1.145节点新增jvmRoute参数:

# cd /usr/local/tomcat/conf

# cp server.xml server.xml.bak

# vim server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost" jvmRoute="TomcatB">

6、分别启动192.168.1.144节点和192.168.1.145节点上的Tomcat

# catalina.sh stop  # catalina.sh configtest  # catalina.sh start  # ss -tunlp | grep -w :8009

7、192.168.1.143节点安装Apache实现反代、动静分离和负载均衡

# yum -y install httpd

# cd /etc/httpd/conf

# cp httpd.conf httpd.conf.bak

# vim httpd.conf

Ø  新增代码:ServerName localhost:80

Ø  注释代码:DocumentRoot "/var/www/html"

# httpd -t  # systemctl start httpd.service  # ss -tunlp | grep -w :80

# httpd -M | grep proxy

image.png

(1)配置status状态页面:

# vim /etc/httpd/conf/httpd.conf,末尾新增如下代码:

<Location /server-status>

SetHandler server-status

Require all granted

</Location>

备注:此处未做访问控制,status状态页面用于测试反代功能

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:

192.168.1.143

image.png

192.168.1.143/server-status

image.png

(2)配置虚拟主机:# vim /etc/httpd/conf.d/vhosts.conf

<Proxy balancer://testcluster>

BalancerMember ajp://192.168.1.144:8009 loadfactor=1 route=TomcatA

BalancerMember ajp://192.168.1.145:8009 loadfactor=1 route=TomcatB

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost Off

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

</VirtualHost>

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

备注:常用指令说明

Ø  ProxyVia On:每个请求和响应报文的HTTP首部均添加Via,用于在多级代理中控制代理请求的流向

Ø  ProxyRequests Off:关闭Apache正向代理功能,开启反代功能必须关闭正向代理功能

Ø  ProxyPreserveHost Off:启用此功能,代理会将用户请求报文中的Host:行发送给后端服务器,而不再使用ProxyPass指定的服务器地址,如果想在反代中支持虚拟主机,则需要开启此项

Ø  ProxyPass PATH !|URL:将后端服务器某URL与当前服务器的某虚拟路径关联起来作为提供服务的路径,PATH为当前服务器上的某虚拟路径,URL为后端服务器上某URL路径,使用此指令时必须将ProxyRequests的值设置为Off,而且如果PATH/结尾,则对应的URL也必须以/结尾,反之亦然

Ø  ProxyPa***everse:让Apache调整HTTP重定向响应报文中的LocationContent-LocationURI标签所对应的URL,在反代环境中必须使用此指令避免重定向报文绕过Proxy服务器

(3)本地浏览器访问:

192.168.1.143

image.png

192.168.1.143/server-status

image.png

192.168.1.143/test.jsp

image.png

image.png

轮询显示,且Session ID一直在变:

image.png

image.png

8、 配置proxy_balancer_module内置的Load Balancer Manager# vim /etc/httpd/conf.d/vhosts.conf

<Proxy balancer://testcluster>

BalancerMember ajp://192.168.1.144:8009 loadfactor=1 route=TomcatA

BalancerMember ajp://192.168.1.145:8009 loadfactor=1 route=TomcatB

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost Off

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass /balancer-manager !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

<Location /balancer-manager>

SetHandler balancer-manager

Require all granted

</Location>

</VirtualHost>

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:192.168.1.143/balancer-manager

image.png

9、实现session会话保持:# vim /etc/httpd/conf.d/vhosts.conf

Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED

<Proxy balancer://testcluster>

BalancerMember ajp://192.168.1.144:8009 loadfactor=1 route=TomcatA

BalancerMember ajp://192.168.1.145:8009 loadfactor=1 route=TomcatB

ProxySet lbmethod=bytraffic stickysession=ROUTEID

</Proxy>

<VirtualHost *:80>

ServerName web.qiuyue.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost Off

<Proxy *>

Require all granted

</Proxy>

ProxyPass /server-status !

ProxyPass /balancer-manager !

ProxyPass / balancer://testcluster/

ProxyPa***everse / balancer://testcluster/

<Location />

Require all granted

</Location>

<Location /balancer-manager>

SetHandler balancer-manager

Require all granted

</Location>

</VirtualHost>

备注:lbmethodApache实现负载均衡的调度方法,默认为byrequests,即基于请求次数进行调度;bytraffic为基于流量进行调度;bybusyness为基于繁忙程序进行调度。

# httpd -t  # systemctl reload httpd.service  # ss -tunlp | grep -w :80

本地浏览器访问:

192.168.1.143/test.jsp无论如何刷新,页面都不会改变

360安全浏览器F12

image.png

备注:关闭浏览器,重新打开并访问192.168.1.143/test.jsp,会基于新的Cookie进行会话保持

192.168.1.143/balancer-manager

image.png

 


VPS岛 的文章和资源来自互联网,仅作为参考资料,如果有侵犯版权的资源请尽快联系站长,我们会在24h内删除有争议的资源。丨 转载请注明Apache + Tomcat基于AJP协议实现反代、动静分离、负载均衡和session会话保持
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址