Nginx+Tomcat-cluster构建
-----------ReProxy-------------------------Client-----------
192.168.56.202 nginx 192.168.56.200 Tomcat
|
192.168.56.201 Tomcat
1、配置Tomcat:注:Client 两个客户端配置相同
安装JDK
[root@localhost ~]# tar -zxvf jdk-7u65-linux-x64.gz
[root@localhost ~]# mv jdk1.7.0_65/ /usr/local/java
[root@localhost ~]# which java
[root@localhost ~]# rm -rf /usr/bin/java //删除redhat集成的java
[root@localhost ~]# ln -s /usr/local/java/bin/* /usr/bin/
[root@localhost ~]# java -version
[root@localhost ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost ~]# source /etc/profile.d/java.sh
[root@localhost ~]# echo $PATH
[root@localhost ~]# java -version
安装Tomcat
[root@localhost ~]# tar -zxvf apache-tomcat-7.0.54.tar.gz
[root@localhost ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7
[root@localhost ~]# /usr/local/tomcat7/bin/startup.sh
[root@localhost ~]# netstat -anput | grep 8080
[root@localhost ~]# mkdir -p /web/benet
[root@localhost ~]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title> JSP test1 page</title>
</head>
<body>
<% out.println("Welcome to test site,http://www.test1.com");%>
</body>
</html>
[root@localhost ~]# vim /usr/local/tomcat7/conf/server.xml
Host标签内添加:
<Context docBase="/web/benet" path="" reloadable="false">
</Context>
[root@localhost ~]# /usr/local/tomcat7/bin/shutdown.sh //关闭服务
[root@localhost ~]# /usr/local/tomcat7/bin/startup.sh //开启服务
2、配置Nginx反向代理
安装Nginx
[root@localhost ~]# yum install pcre-devel zlib-devel -y
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost ~]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost ~]# nginx -t //初始化nginx
[root@localhost ~]# nginx //启动Nginx
[root@localhost ~]# netstat -anput | grep 80
[root@localhost ~]# vim nginx.conf
动静分离:
location ~ \.jsp$ {
proxy_pass 192.168.56.200:8080;
}
测试:http://192.168.56.202 访问Nginx提供的静态页面
http://192.168.56.202/index.jsp 访问Tomcat提供的JAVA动态页面
Tomcat集群cluster:
http模块内--定义服务器
upstream myCluster {
server 192.168.56.200:8080 weight=5;
server 192.168.56.201:8080;
}
server模块内--定义集群
location ~ \.jsp$ {
proxy_pass http://myCluster; //于上面cluster定义名称相同
proxy_redirect off;
proxy_set_header Host $host; //返回主机名
proxy_set_header X-Real-IP $remote_addr; //真实ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //启动转发
}
测试:http://192.168.56.202/index.jsp 刷新页面后会反复出现server1和server2