httpd或Nginx负载均衡tomcat

实验环境:CentOS7

#两台tomcat的基本配置如下:
[root@webapps localhost]#setenforce 0
[root@webapps localhost]#iptables -F
[root@webapps localhost]#yum -y install java-1.8.0-openjdk-devel tomcat tomcat-webapps tomcat-admin-webapps
[root@webapps localhost]#chown tomcat.tomcat /usr/share/tomcat/webapps/test/*
[root@webapps localhost]#chown tomcat.tomcat test

[root@webapps localhost]#vim /etc/tomcat/server.xml 
    <Connector port="8081" protocol="HTTP/1.1" maxThreads="200" enableLookups="False"
               connectionTimeout="60000"  address="172.16.252.142" 
               redirectPort="8443" />

[root@webapps localhost]#vim /usr/share/tomcat/webapps/test/index.jsp

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
        <head><title>tomcat1 mini</title></head>
        <body>
                <%
                        out.println("tomcat1 mini");
                %>
        </body>
</htmL>

[root@webapps localhost]#vim /usr/share/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="123456" roles="manager-gui"/>

 

 #第三台主机安装Nginx:172.16.253.177

 #ngixn做负载均衡:

[root@~ localhost]#setenforce 0
[root@~ localhost]#iptables -F
[root@~ localhost]#yum -y install nginx-1.10.2-1.el7.ngx.x86_64.rpm 
[root@~ localhost]#vim /etc/nginx/nginx.conf
http {
#定义:集群tomcatsr
upstream tomcatsr {
        server 172.16.252.142:8081;
        server 172.16.254.47:8081;
}
    include /etc/nginx/conf.d/*.conf;
}
#引用
[root@~ localhost]#vim /etc/nginx/conf.d/default.conf
server {
   location / {
        proxy_pass http://tomcatsr;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

#httpd做负载均衡:

 [root@~ localhost]#yum -y install httpd

 [root@~ localhost]#httpd -M

  proxy_balancer_module (shared)#负载均衡模块

[root@~ localhost]#vi /etc/httpd/conf.d/tomcat-httpd.conf

<proxy balancer://tomcatsr>
        BalancerMember http://172.16.252.142:8081
        BalancerMember http://172.16.254.47:8081
        ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
        ServerName linux.io
        ProxyVia On
        ProxyRequests Off
        ProxyPreserveHost On
        <Proxy *>
        Require all granted
        </Proxy>
        ProxyPass / balancer://tomcatsr/
        ProxyPassReverse / balancer://tomcatsr/
        <Location />
        Require all granted
        </Location>
</VirtualHost>

#测试:

[root@~ localhost]#for i in {1..10};do curl http://172.16.253.177/test/;done


<html>
    <head><title>tomcat2 mini1</title></head>
    <body>
        tomcat2 mini1

    </body>
</htmL>


<html>
    <head><title>tomcat1 mini</title></head>
    <body>
        tomcat1 mini

    </body>
</htmL>


<html>
    <head><title>tomcat2 mini1</title></head>
    <body>
        tomcat2 mini1

    </body>
</htmL>


<html>
    <head><title>tomcat1 mini</title></head>
    <body>
        tomcat1 mini

    </body>
</htmL>


<html>
    <head><title>tomcat2 mini1</title></head>
    <body>
        tomcat2 mini1

    </body>
</htmL>


<html>
    <head><title>tomcat1 mini</title></head>
    <body>
        tomcat1 mini

    </body>
</htmL>


<html>
    <head><title>tomcat2 mini1</title></head>
    <body>
        tomcat2 mini1

    </body>
</htmL>


<html>
    <head><title>tomcat1 mini</title></head>
    <body>
        tomcat1 mini

    </body>
</htmL>


<html>
    <head><title>tomcat2 mini1</title></head>
    <body>
        tomcat2 mini1

    </body>
</htmL>


<html>
    <head><title>tomcat1 mini</title></head>
    <body>
        tomcat1 mini

    </body>
</htmL>
原文地址:https://www.cnblogs.com/wzhuo/p/7137701.html