* * Experimental Environment * *
CentOS 7
Haproxy (Nginx): 172.16.61.4 (NODE4)
httpd+tomcat:172.16.61.7 (Node7)
172.16.61.8 (Node8)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/7D/34/wKiom1biSr7xF0_-AACaR_ESkoo427.jpg "title=" 1.jpg " alt= "Wkiom1bisr7xf0_-aacar_eskoo427.jpg"/>
First, using Nginx to do load balancing
* * Configuring Tomcat and httpd**
<engine name= "Catalina" defaulthost= "localhost" jvmroute= "Tomcat8" > # Add Jvmroute tags to the node8 node for intuitive viewing of load balancing effects (Node7 node is Tomcat7) * * Configuration nginx**
Upstream Tcsrvs {#在nginx配置文件中定义upsteam模块, the default polling scheduling algorithm server node8:80; #基于主机名调度, the local hosts file to parse back-end hostname server node7:80; }location/{#在server中定义location进行反向代理 proxy_set_header Host $http _host; #将客户端请求的host代理至后端 Proxy_pass http://tcsrvs/; }
Configure local Hosts file, resolve 172.16.61.4 node8.tz.com address
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/35/wKiom1biV1jRWQdrAAA9sbYVxC0897.png "style=" float: none; "title=" 1.PNG "alt=" Wkiom1biv1jrwqdraaa9sbyvxc0897.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7D/34/wKioL1biV97i47hwAAA-RcShikk605.png "style=" float: none; "title=" 2.PNG "alt=" Wkiol1biv97i47hwaaa-rcshikk605.png "/>
Realize the effect of polling
**haproxy Load Balancing tomcat**
[[email protected] haproxy]# sed ' [Email protected]^#.*\+\|^[[:space:]]\+#.*\[email protected]@ ' haproxy.cfgglobal 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/statsdefaults 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 &nbsP;timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000frontend main *:80 default_backend tomcatlisten statisticsbind *:9090stats enablestatshide-versionstats uri /haproxyadmin?statsstats realm "Haproxy\ statistics" stats auth admin:tianzhuangstats Adminif truebackend toMcat balance roundrobin server tomcat1 172.16.61.7:80 check Servertomcat2 172.16.61.8:80 check
**HTTPD Load Balancer Configuration * *
<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>
* * Keep Session Sessions * *
Upstream Tcsrvs {server node8:80; Server node7:80; Ip_hash; #基于客户端地址进行hash, the request from the same IP is sent to the same server# hash $request _uri; #基于请求的uri hash, applied to the backend cache server;# hash $remote _addr; #基于远程客户端地址hash, equivalent to Ip_hash; }
Backend Tomcat balance Roundrobin cookie Tomcat insert NoCache #在后端主机的响应报文中插入cookie server to MCAT1 172.16.61.7:80 Check Cookie tomcat1 server tomcat2 172.16.61.8:80 Check cookie TOMCAT2
[[email protected] conf.d]# cat proxy.conf header add set-cookie " routeid=.%{balancer_worker_route}e; path=/" env=BALANCER_ROUTE_CHANGED #增加头部的session信息 with route as the keyword <proxy balancer:// tccluster>balancermember http://172.16.61.7:80 loadfactor=1 route=tomcat7 # The route is TOMCAT7, and the backend tomcat server needs to add the route information balancermember http://172.16.61.8:80 loadfactor=1 route=Tomcat8ProxySet lbmethod=bytrafficProxySet stickysession=ROUTEID</proxy>< virtualhost *:80> servername localhostproxyrequests offproxyvia onproxypass / balancer://tccluster/ proxypassreverse / balancer://tccluster/proxypreservehost on<proxy *> Require all granted</proxy><location /> Require all granted</Location></VirtualHost>
Haproxy, Nginx, and httpd load balancing tomcat host, seesion retention issues