环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1》nginx反代
1、在B和C上的设置
mkdir -pv
/usr/share
/tomcat/webapps
/myapp/WEB-INF
vim
/usr/share
/tomcat/webapps
/myapp/index.jsp
<%@ page language="java" %>
<%@ page language="java" %>
<html>
<head><title>TomcatA
</title></head>
<body>
<h1><font color="red">TomcatA.com
</font></h1> ##在C上将颜色改为green,Tomcat改为B
<tablehljs-number">1">
<tr>
<td>Session ID
</td>
<% session.setAttribute("shen.com","shen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on
</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
http://172.18.213.251:8080/myapp/
http://172.18.213.254:8080/myapp/
2、A上设置
vim
/etc/nginx/nginx.conf
在http中配置
upstream
tomcatsrv {
server
192.168.213.251:
8080 ;
server
192.168.213.254:
8080 ;
}
server {
listen
80 default_server;
listen
[::]:
80 default_server;
index index.jsp ;
root
/usr/share
/tomcat/webapps/app;
location
/ {
proxy_pass http://tomcatsrv ;
}
service nginx restart
http://172.18.213.253/myapp/
2》httpd反代
1、在B和C上的设置同上
2、在调度器上的设置
vim
/etc/httpd/conf.d/tomcat.conf
<proxy balancer://tomcatsrvs>
##定义一个后端服务器组
BalancerMember http:
//192.168.213.251:8080 ##如果和后端服务器连接的协议为ajp协议,把http改为ajp并且把端口改为8009即可
BalancerMember http:
//192.168.213.254:8080
ProxySet lbmethod=byrequests
</proxy>
namevirtualhost *:
80
<VirtualHost *:80>
documentroot /app
<Directory /app>
Require all granted
</Directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer:
//tomcatsrvs/
ProxyPassReverse / balancer:
//tomcatsrvs/
</VirtualHost>
httpd -t
service httpd start
http://172.18.213.253/app/
httpd的负载集群功能具有健康状态检查功能
可以把后端的一个tomcat停掉
然后访问http:
//172.18.213.253/app/
会发现不往关闭的tomcat主机调度了
5.实现httpd和nginx的会话粘性绑定
环境
A:nginx和httpd:192.168.213.253
B:tomcat:192.168.213.251
C:tomcat:192.168.213.254
1)httpd会话绑定
在B和C上设置
vim /etc/tomcat/server.xml
<Engine name=
"Catalina" defaultHost=
"localhost" jvmroute=
"tomcatA">
##在B上此行增加一个jvmroute="tomcatA,在C上此行增加jvmroute="tomcatB"
systemctl restart tomcat
在A上设置
vim /etc/httpd/conf.d/tomcat.conf
Header add Set-Cookie
"ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer:
//tomcatsrvs>
BalancerMember http:
//172.18.213.251:8080 route=tomcatA
BalancerMember http:
//172.18.213.254:8080 route=tomcatB
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
namevirtualhost *:
80
<VirtualHost *:
80>
documentroot /app
<directory /app>
Require all granted
</directory>
ProxyVia On
ProxyRequests Off
ProxyPass / balancer:
//tomcatsrvs/
ProxyPassReverse / balancer:
//tomcatsrvs/
</VirtualHost>
service httpd reload
http:
//172.18.213.253/app/