haproxynginx以及httpd负载均衡tomcat主机,seesion保持问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxynginx以及httpd负载均衡tomcat主机,seesion保持问题相关的知识,希望对你有一定的参考价值。
**实验环境**
CentOS 7
haproxy(nginx):172.16.61.4(node4)
httpd+tomcat:172.16.61.7(node7)
172.16.61.8(node8)
一、使用nginx做负载均衡
**配置tomcat以及httpd**
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat8"> #在node8节点添加jvmRoute标记,便于直观查看负载均衡效果(node7节点为Tomcat7) <Host name="node8.tz.com" appBase="/data/webapps" #在两台tomcat上配置同样的虚拟主机 unpackWARs="true" autoDeploy="true"> [[email protected] conf.d]# cat proxy.conf #httpd配置(两台tomcat要相同) <VirtualHost *:80> ServerName tc1.tz.com ProxyRequests Off ProxyVia On ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp:://localhost:8009/ <Location /> Require all granted </Location> </VirtualHost> [[email protected] ROOT]# cat index.jsp #在tomcat的虚拟主机中定义主页内容 <%@ page language="java" %> <html> <head><title>Tomcat7</title></head> <body> <h1><font color="red">node8.tz.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
**配置nginx**
upstream tcsrvs { #在nginx配置文件中定义upsteam模块,默认采用轮询的调度算法 server node8:80; #基于主机名调度,本地hosts文件要解析后端hostname server node7:80; } location / { #在server中定义location进行反向代理 proxy_set_header Host $http_host; #将客户端请求的host代理至后端 proxy_pass http://tcsrvs/; }
配置本地hosts文件,解析172.16.61.4 node8.tz.com地址
实现了轮询的效果
**haproxy负载均衡tomcat**
[[email protected] haproxy]# sed ‘[email protected]^#.*\+\|^[[:space:]]\+#.*\[email protected]@‘ haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main *:80 default_backend tomcat listen statistics bind *:9090 stats enable stats hide-version stats uri /haproxyadmin?stats stats realm "HAproxy\ Statistics" stats auth admin:tianzhuang stats admin if TRUE backend tomcat balance roundrobin server tomcat1 172.16.61.7:80 check server tomcat2 172.16.61.8:80 check
**httpd负载均衡配置**
<proxy balancer://tccluster> BalancerMember http://172.16.61.7:80 loadfactor=1 BalancerMember http://172.16.61.8:80 loadfactor=1 ProxySet lbmethod=bytraffic </proxy> <VirtualHost *:80> ServerName localhost ProxyRequests Off ProxyVia On ProxyPass / balancer://tccluster/ ProxyPassReverse / balancer://tccluster/ ProxyPreserveHost On <Proxy *> Require all granted </Proxy> <Location /> Require all granted </Location> </VirtualHost>
**保持session会话**
nginx负载均衡session保持问题:
upstream tcsrvs { server node8:80; server node7:80; ip_hash; #基于客户端地址进行hash,将来自同一ip的请求发至同一个server # hash $request_uri; #基于请求的uri hash,应用于后端的缓存server; # hash $remote_addr; #基于远程客户端地址hash,相当于ip_hash; }
haproxy负载均衡session保持问题:
backend tomcat balance roundrobin cookie TOMCAT insert nocache #在后端主机的响应报文中插入cookie server tomcat1 172.16.61.7:80 check cookie tomcat1 server tomcat2 172.16.61.8:80 check cookie tomcat2
httpd负载均衡session保持问题:
[[email protected] conf.d]# cat proxy.conf Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED #增加头部的session信息,以route为关键字 <proxy balancer://tccluster> BalancerMember http://172.16.61.7:80 loadfactor=1 route=Tomcat7 #route为Tomcat7,后端tomcat server也需要加入该route信息 BalancerMember http://172.16.61.8:80 loadfactor=1 route=Tomcat8 ProxySet lbmethod=bytraffic ProxySet stickysession=ROUTEID </proxy> <VirtualHost *:80> ServerName localhost ProxyRequests Off ProxyVia On ProxyPass / balancer://tccluster/ ProxyPassReverse / balancer://tccluster/ ProxyPreserveHost On <Proxy *> Require all granted </Proxy> <Location /> Require all granted </Location> </VirtualHost>
以上是关于haproxynginx以及httpd负载均衡tomcat主机,seesion保持问题的主要内容,如果未能解决你的问题,请参考以下文章