1.安装tomcat
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/lnmp/
cd /usr/local/lnmp/
ln -s apache-tomcat-7.0.37/ tomcat
cd tomcat/
bin/startup.sh
vim /usr/local/lnmp/nginx/conf/nginx.conf
在location ~ \.php$之后,添上以下内容(82行之后)
location ~ \.jsp$ {
proxy_pass http://172.25.45.1:8080;
}
nginx -s reload
cd /usr/local/lnmp/tomcat/webapps/ROOT
vim test.jsp
The time is:<%=new java.util.Date()%>
2.tomcat 集群
再启动一个虚拟机server2,把配置好的java-jdk和tomcat复制到启动的server2中
【server1】
scp -r /usr/local/java/ root@172.25.45.2:/usr/local/
scp -r /usr/local/lnmp/tomcat/ root@172.25.45.2:/usr/local/
scp /etc/profile root@172.25.45.2:/etc/
【server2】
source /etc/profile
echo $PATH
cd /usr/local/tomcat
bin/startup.sh
访问server2的tomcat
3.rr论寻
【server1】
vim /usr/local/lnmp/nginx/conf/nginx.conf
16 http {
17 upstream westos{
18 server 172.25.45.1:8080;
19 server 172.25.45.2:8080;
20 }
21
修改server后的端口值为8080
81 location ~ \.jsp$ {
82 proxy_pass http://westos;
83 }
将proxy_pass改为http://westos
nginx -t
nginx -s reload
cd /usr/local/lnmp/tomcat/webapps/ROOT
vim test.jsp
在内容最前面加上server1-
【server2】
cd /usr/local/tomcat/webapps/ROOT
对server2的test.jsp也进行修改,在内容最前面加上server2-
访问172.25.45.1/test.jsp
页面显示会在server1-The time与The time之间切换
关闭server1的tomcat,只显示server2的/usr/local/lnmp/tomcat/webapps/ROOT/test.jsp 内容
4.ip_hash
vim /usr/local/lnmp/nginx/conf/nginx.conf
在17行upstream westos段下添上ip_hash;
同一个ip访问的是同一台服务器
./shutdown.sh
关闭一台后访问另外一台
5.sticky模式
从新编译1.8版本的nginx
tar zxf nginx-1.8.0.tar.gz
tar zxf nginx-sticky-module-1.0.tar.gz
./configure \
--prefix=/usr/local/lnmp/nginx \
--with-http_ssl_module --with-http_stub_status_module \
--add-module=/mnt/nginx-sticky-module-1.0
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-1.0
vim /usr/local/lnmp/nginx/conf/nginx.conf
在17行upstream westos段下注释掉ip_hash添上sticky
vim /usr/local/lnmp/tomcat/webapps/ROOT/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
sticky 模式当用户访问,只要访问主机tomcat不挂,就一直访问同一个
vim /usr/local/lnmp/nginx/conf/nginx.conf
注释掉sticky
nginx -t
nginx -s reload
【server1】
vim /usr/local/lnmp/nginx/conf/nginx.conf
取消sticky的注释
nginx -t
nginx -s reload
【server2】
cd /usr/local/tomcat/logs
tail -f catalina.out
cd /usr/local/tomcat
bin/shutdown.sh
server2关闭,访问server1
原文地址:http://wjl19940429.blog.51cto.com/11354210/1788984