httpd+tomcat(1) -- proxy_http_module

全部系统采用centos7.1

各节点配置好hosts文件
cat /etc/hosts
172.16.92.2    node2.centos7.com    node2
172.16.92.3    node3.centos7.com    node3
172.16.92.4    node4.centos7.com    node4

node2, node3  --> tomcat ( jsp网站服务器 )
node4 --> httpd(proxy_http_module) 反向代理至tomcat

================ node2 172.16.92.2/16 ===================
安装JDK(光盘源)
mount /dev/cdrom /mnt/cdrom/
yum -y install java-1.7.0-openjdk

安装二进制tomcat

wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.23/bin/apache-tomcat-8.0.23.tar.gz
tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local
cd /usr/local
ln -sv apache-tomcat-8.0.23 tomcat
vim /etc/profile.d/tomcat.sh
(:set paste)
export CATALINA_HOME=/usr/local/tomcat
export PATH=$PATH:$CATALINA_HOME/bin
================
. /etc/profile.d/tomcat.sh
catalina.sh --help
catalina.sh version
catalina.sh start

windows浏览器访问 http://172.16.92.2:8080/ 能看到tomcat首页即可

catalina.sh stop

mkdir -pv /data/webapps/ROOT
cd tomcat/conf
cp server.xml{,.bak}
vim server.xml
#添加虚拟主机
<Engine name="Catalina" defaultHost="node2.centos7.com">   #修改
:set nu
:123,139y  -> p -> 修改成如下
      <Host name="node2.centos7.com"  appBase="/data/webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="" docBase="/data/webapps/ROOT" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
               prefix="tomcatA_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
==============
mkdir /data/logs
catalina.sh configtest
>全部都是INFO开头的就正常了

(难道是把jar放在 /data/webapps/ROOT/lib 下?)

mkdir -pv /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
vim /data/webapps/ROOT/index.jsp

<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA.centos7.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("centos7.com","centos7.com"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
================
catalina.sh start
ss -tnal | grep 8080

windows 浏览器访问 http://172.16.92.2:8080/
--------------------------------------------------------------------
TomcatA.centos7.com
Session ID     D9D258F305C067821340427EEC9BDE25
Created on     1446019812559
--------------------------------------------------------------------



================ node3 172.16.92.3/16 ===================
安装JDK(光盘源)
mount /dev/cdrom /mnt/cdrom/
yum -y install java-1.7.0-openjdk

安装tomcat
scp node2:/root/apache-tomcat-8.0.23.tar.gz ./
tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local
cd /usr/local
ln -sv apache-tomcat-8.0.23 tomcat
scp node2:/etc/profile.d/tomcat.sh /etc/profile.d/
. /etc/profile.d/tomcat.sh
catalina.sh --help
catalina.sh version
catalina.sh start

windows浏览器访问 http://172.16.92.3:8080/
能看到tomcat首页即可

catalina.sh stop

cd /usr/local/tomcat/conf/
cp server.xml{,.bak}
scp node2:/usr/local/tomcat/conf/server.xml ./
vim server.xml
<Engine name="Catalina" defaultHost="node3.centos7.com">              #修改
<Host name="node3.centos7.com"  appBase="/data/webapps" autoDeploy="true">    #修改
prefix="tomcatB_access_log" suffix=".txt"                         #修改
=================
mkdir -pv /data/webapps/ROOT/{lib,classes,META-INF,WEB-INF}
mkdir /data/logs
vim /data/webapps/ROOT/index.jsp

<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB.centos7.com</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("centos7.com","centos7.com"); %>        
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>    </table>
  </body>
</html>
==========
catalina.sh configtest
>全部都是INFO开头的就是正常了
catalina.sh start

windows 浏览器访问 http://172.16.92.3:8080/
(第一次访问时间会比较长)
-----------------------------------------------------------------------
TomcatB.centos7.com
Session ID     AD6C6BE89A883FC0C2D881CABC9ABD9C
Created on     1446021071150
-----------------------------------------------------------------------



================ node4 172.16.92.4/16 ===================
apache做tomcat反向代理方式一 --- proxy_http_module

(光盘yum源)
mount /dev/cdrom /mnt/cdrom
yum -y install httpd
httpd -M | grep proxy_http_module

vim /etc/httpd/conf/httpd.conf
ServerName node4.centos7.com:80    #去注释并更改
#DocumentRoot "/var/www/html"    #注释中心主机
========================
vim /etc/httpd/conf.d/vhosts_http.conf

<proxy balancer://lbcluster1>
    BalancerMember http://172.16.92.2:8080 loadfactor=10 route=TomcatA
    BalancerMember http://172.16.92.3:8080 loadfactor=10 route=TomcatB
</proxy>
<VirtualHost *:80>
    ServerName tomcat.centos7.com
    ProxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / balancer://lbcluster1/
    ProxyPassReverse / balancer://lbcluster1/
    <Location />
        Require all granted
    </Location>
</VirtualHost>
====================
httpd -t
systemctl start httpd.service
ss -tanlp | grep 80





windows浏览器访问 http://172.16.92.4/index.jsp ,在如下两个网页轮询
--------------------------------------------------------------------------------
TomcatA.centos7.com
Session ID     DACF1B67D1F06A4F1181696C6F36B226
Created on     1446021532446
--------------------------------------------------------------------------------
TomcatB.centos7.com
Session ID     FBCF88D46F56EC8DD7153095C16A08BF
Created on     1446021537042
--------------------------------------------------------------------------------





================ node4 172.16.92.4/16 ===================
apache基于session实现会话绑定

vim /etc/httpd/conf.d/vhosts_http.conf
#添加第一行和第五行
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
        BalancerMember http://172.16.92.2:8080 loadfactor=10 route=TomcatA
        BalancerMember http://172.16.92.3:8080 loadfactor=10 route=TomcatB
        ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>    #此容器内的保持不变
......
</VirtualHost>
=================
最终修改后结果如下:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
    BalancerMember http://172.16.92.2:8080 loadfactor=10 route=TomcatA
    BalancerMember http://172.16.92.3:8080 loadfactor=10 route=TomcatB
            ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
    ServerName tomcat.centos7.com
    ProxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / balancer://lbcluster1/
    ProxyPassReverse / balancer://lbcluster1/
    <Location />
        Require all granted
    </Location>
</VirtualHost>
=================
httpd -t
systemctl reload httpd

windows浏览器访问 http://172.16.92.4/index.jsp, 绑定在如下页面了
TomcatA.centos7.com
Session ID     C69703CAD1B76C003EBDD33B2C744A25
Created on     1446021766794


#####################################
apache状态管理页
vim /etc/httpd/conf.d/vhosts_http.conf
......                    #内容一样滴
<VirtualHost *:80>
.......
        <Location /balancer-manager>        #添加此段
                SetHandler balancer-manager
                Proxypass !
                Require all granted            #允许所有的客户端访问
                #Require ip 172.16.249.102        #只允许172.16.249.102访问
        </Location>
</VirtualHost>
=================
最终修改后结果如下:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
    BalancerMember http://172.16.92.2:8080 loadfactor=10 route=TomcatA
    BalancerMember http://172.16.92.3:8080 loadfactor=10 route=TomcatB
            ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost *:80>
    ServerName tomcat.centos7.com
    ProxyVia On
    ProxyRequests Off
    ProxyPreserveHost On
    <Proxy *>
        Require all granted
    </Proxy>
    ProxyPass / balancer://lbcluster1/
    ProxyPassReverse / balancer://lbcluster1/
    <Location />
        Require all granted
    </Location>
            <Location /balancer-manager>        
                    SetHandler balancer-manager
                    Proxypass !
                    Require all granted            
                    #Require ip 172.16.249.102        
            </Location>
</VirtualHost>
=================
httpd -t
systemctl reload httpd.service

windows浏览器访问 http://172.16.92.4/balancer-manager, 出现如下页面,则说明成功!
>Load Balancer Manager for 172.16.92.4
>Server Version: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9
>Server Built: Mar 12 2015 15:07:19
>......
基于此页面可做网站应用的各项设置和上下线操作, 实现灰度发布.

你可能感兴趣的:(tomcat,httpd)