1台 redhat 32 位配置nginx + tomcat2+MSM实现负载均衡,动静分离

1. rpm -ivh jdk-7u51-linux-i586.rpm
2.JDK 环境变量配置

vi  /etc/profile
最后增加

#set environment for JDK
export JAVA_HOME=/usr/java/jdk1.7.0_51
export CLASSPATH=.:$JAVA_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH

3.配置完成之后,自己验证命令 javac和java
4.tar -zxvf apache-tomcat-7.0.52.tar.gz 解压之后copy一份
5.cp -r apache-tomcat-7.0.52 apache-tomcat-7.0.53 因为是同一台电脑,需要改动其中1台tomcat的端口信息
6. 
因为我安装在 /usr/local下面,可以用下面命令开启和关闭,验证tomcat是否正常启动 也可以通过如下命令查看 ps -ef | grep java
/usr/local/apache-tomcat-7.0.52/bin/shutdown.sh 
/usr/local/apache-tomcat-7.0.53/bin/shutdown.sh 
/usr/local/apache-tomcat-7.0.52/bin/startup.sh 
/usr/local/apache-tomcat-7.0.53/bin/startup.sh 
7.下面安装nginx,我安装nginx方式比较简单,使用yum方式安装,如果有任何问题,可以跟我说,我会马上回复你
redhat 因为yum无法用,但是我重新改变了一下,可以查看如下链接
http://blog.csdn.net/zcyhappy1314/article/details/17580943
但是有一个地方需要改变一下,就是链接地址
wget http://tel.mirrors.163.com/centos/6/os/i386/Packages/yum-3.2.29-40.el6.centos.noarch.rpm
wget http://tel.mirrors.163.com/centos/6/os/i386/Packages/yum-metadata-parser-1.1.2-16.el6.i686.rpm
wget http://tel.mirrors.163.com/centos/6/os/i386/Packages/yum-plugin-fastestmirror-1.1.30-14.el6.noarch.rpm 
wget http://tel.mirrors.163.com/centos/6/os/i386/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm
8.nginx 配置信息如下

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
  upstream  tomcat_cluster{
              server   192.168.1.167:8080;
              server   192.168.1.167:8081;
    }
  server {
    listen 80;
    server_name 192.168.1.167;
    server_name_in_redirect off;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  
    location  / {
      proxy_pass http://tomcat_cluster; #转向tomcat处理
    }

    location ~ \.(jsp|do|action)$ { #所有jsp页面以及do/action请求均交由tomcat处理
      proxy_pass http://tomcat_cluster; #转向tomcat处理
    }

   location ~ \.(htm|html|gif|jpg|jpeg|png|ico|rar|css|js|zip|txt|flv|swf|doc|ppt|xls|pdf)$ {
            root  /home/leiwente/webapp/ROOT;
            expires      30h;
       }
   }
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}




9.先删除两个tomcat下面ROOT下面所有的文件,在其中一个tomcat新建 vi test.jsp

SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 111111");
%>

在另外一个tomcat 新建 vi test.jsp

SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
out.println("This is Tomcat Server 222222");
%>



10.直接访问 http://192.168.1.127/test.jsp页面的再动态切换
11.下面要做session的同步,我使用的memcached-session-manager,注意使用版本问题,其中有很多序列化方式,我使用的是spy,一些文中的包都可以从下面的链接文章中下载到
https://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
注意序列化包的版本号,我的包如下图,反正 每一个tomcat的lib下面
1台 redhat 32 位配置nginx + tomcat2+MSM实现负载均衡,动静分离_第1张图片
12.memcached安装直接使用yum
搜索 yum search memcached
安装 yum install  memcached.i686
启动 service memcached restart
13.客户可以通过本机登入cmd telnet 192.168.1.167 11211 登入 其中ip为你装的机器
1台 redhat 32 位配置nginx + tomcat2+MSM实现负载均衡,动静分离_第2张图片


14.每一个tomcat中还需要配置context.xml还需要配置manager

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:192.168.1.167:11211"
    sticky="false"
    sessionBackupAsync="false"
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />
    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>
~



15.注意这里还有1个小问题,因为1台服务器上面安装2台tomcat,涉及到nginx处理图片,js,css路径的问题
16.这里使用建立一个统一代码存放的地点,例如 /home/leiwente/webapp下面

17 在每一个tomcat下面ROOT下面,已经删除了ROOT下面所有的文件 ln -s /home/leiwente/webapp/ROOT/  ROOT
18以后只要更新 / home/leiwente/webapp 下面,两个tomcat会同时更新代码
19现在开始重新启动每一个tomcat信息,下面是其中一个tomcat信息
1台 redhat 32 位配置nginx + tomcat2+MSM实现负载均衡,动静分离_第3张图片

20直接访问 http://192.168.1.167/test.jsp页面
sessionId是不会发生改变的








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