Nginx+tomcat负载均衡

Posted 夕颜@伤雨痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx+tomcat负载均衡相关的知识,希望对你有一定的参考价值。

实验环境

[root@Java-JDK ~]# tar zxvf jdk-7u65-linux-x64.gz

[root@Java-JDK ~]# mkdir /usr/java
[root@Java-JDK ~]# mv jdk1.7.0_65/ /usr/java/

[root@Java-JDK ~]# /usr/java/jdk1.7.0_65/bin/java -verssion
Unrecognized option: -verssion
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
[root@Java-JDK ~]# /usr/java/jdk1.7.0_65/bin/java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)    #Java运行的环境即JRE
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

设置环境变量

[root@Java-JDK ~]# vim /etc/profile 

export JAVA_HOME=/usr/java/jdk1.7.0_65
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin

[root@Java-JDK ~]# java -version
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

安装tomcat

wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz

[root@Java-JDK ~]# tar zxvf apache-tomcat-7.0.81.tar.gz

[root@Java-JDK ~]# mv apache-tomcat-7.0.81 /usr/local/tomcat

root@localhost local]# cp -a tomcat/ tomcat_1 && cp -a tomcat/ tomcat_2   #创建两个tomcat

[root@Java-JDK ~]# /usr/local/tomcat/bin/startup.sh

<Server port="8005" shutdown="SHUTDOWN">    #管理关闭Tomcat的端口

<Connector port="8080" protocol="HTTP/1.1"    #管理tomcat协议浏览访问 

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />    #管理AJP代理协议  (PS:apache与tomcat连接需要借助8009/nginx与tomcat连接直接借助8080即可)

[root@localhost local]# vim tomcat_1/webapps/ROOT/index.jsp 

<html>
<body>
<h1>Tomcat Server Nmae hexun JSP Test Page </h1>
<%=new java.util.Date()%>
<hr color="red">
<h1>www.hexun.net</h1>
</body>
</html>

[root@localhost local]#vim tomcat_2/conf/server.xml

<Server port="8006" shutdown="SHUTDOWN">

<Connector port="8081" protocol="HTTP/1.1"

 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

[root@localhost local]# vim tomcat_2/webapps/ROOT/index.jsp

<html>
<body>
<h1>Tomcat Server Name jfedu JSP Test Page </h1>
<%=new java.util.Date()%>
<hr color="red">
<h1>www.jfedu.net</h1>
</body>
</html>

[root@localhost local]# /usr/local/tomcat_1/bin/startup.sh 

[root@localhost local]# /usr/local/tomcat_1/bin/startup.sh 

[root@localhost html]# vim  /usr/local/nginx/conf/nginx.conf    #在Nginx配置文件中配置方向代理负载均衡后端两个Tomcat虚拟主机

 ip_hash;   #保证会话只转发到后台一台web服务器(ps:如果该机器宕机,便会自动转发到下一台机器)

客户端浏览器访问NginxServerIP进行 刷新浏览访问

 

以上是关于Nginx+tomcat负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

怎么查看nginx是不是负载均衡

Nginx + Tomcat 实现 负载均衡 和 动静分离群集

nginx+2tomcat的负载均衡环境,负载均衡策略用权重形式

负载均衡 | Nginx+Tomcat 动静分离实现负载均衡

nginx+tomcat负载均衡策略

Nginx+Tomcat负载均衡集群