nginx+tomcat负载均衡
Posted 夕颜@伤雨痕
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+tomcat负载均衡相关的知识,希望对你有一定的参考价值。
实验环境:
nginx-server:192.168.20.194
tomcatweb01:192.168.20.150
tomcatweb02:192.168.20.163
#####################################################################
nginx-server:
[root@nginx-server ~]# yum install -y pcre-devel zlib-devel
[root@nginx-server ~]# tar zxvf nginx-1.6.0.tar.gz -C /usr/src/
[root@nginx-server ~]# cd /usr/src/nginx-1.6.0/
[root@nginx-server nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx-server nginx-1.6.0]# make && make install
[root@nginx-server nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx-server nginx-1.6.0]# useradd -M -s /sbin/nologin ngin
[root@nginx-server nginx-1.6.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx-server nginx-1.6.0]# vim /etc/init.d/nginx
[root@nginx-server nginx-1.6.0]# chmod +x /etc/init.d/nginx
[root@nginx-server nginx-1.6.0]# /etc/init.d/nginx start
[root@nginx-server nginx-1.6.0]# netstat -anput | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9899/nginx: master
############################nginxweb平台部署完成###############################################
tomcat-web平台
[root@tomcat-web1 ~]# tar zxvf jdk-7u65-linux-x64.gz -C /usr/src/
[root@tomcat-web1 ~]# cd /usr/src/
[root@tomcat-web1 src]# mv jdk1.7.0_65/ /usr/local/java
[root@tomcat-web1 src]# which java
/usr/bin/java
[root@tomcat-web1 src]# ln -s /usr/local/java/bin/* /usr/bin/
[root@tomcat-web1 src]# vim /etc/profile.d/java.sh
[root@tomcat-web1 src]# source /etc/profile.d/java.sh
[root@tomcat-web1 src]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
[root@tomcat-web1 src]# vim /etc/profile.d/java.sh
You have new mail in /var/spool/mail/root
[root@tomcat-web1 src]# source /etc/profile.d/java.sh
[root@tomcat-web1 src]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
[root@tomcat-web1 src]# 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)
java环境部署完成,接下来安装配置tomcat
[root@tomcat-web1 ~]# tar zxvf apache-tomcat-7.0.54.tar.gz -C /usr/src
[root@tomcat-web1 ~]# cd /usr/src/
[root@tomcat-web1 src]# mv apache-tomcat-7.0.54/ /usr/local/tomcat
[root@tomcat-web1 src]# /usr/local/tomcat/bin/startup.sh
[root@tomcat-web1 ~]# cd /usr/src/
[root@tomcat-web1 src]# mv apache-tomcat-7.0.54/ /usr/local/tomcat
[root@tomcat-web1 src]# /usr/local/tomcat/bin/startup.sh
[root@tomcat-web1 src]# netstat -anput | grep 8080
tcp 0 0 :::8080 :::* LISTEN 27993/java
验证tomcat是否正常访问firefox http://192.168.20.150:8080
[root@tomcat-web1 ~]# mkdir /web/xiaoyu -p
[root@tomcat-web1 ~]# vim /web/xiaoyu/index.jsp
[root@tomcat-web1 ~]# vim /usr/local/tomcat/conf/server.xml
[root@tomcat-web1 ~]# elinks 192.168.20.150:8080 #由此我们tomcat01web页面可以正常运行
############tomcatweb01服务部署完成#################
tomcat web01服务(第二台tomcat服务步骤是完全一样的 ,为了便于区别,仅仅是更改了网页内容)
准备java环境---
[root@tomcat-web2 ~]# tar zxvf jdk-7u65-linux-x64.gz -C /usr/src/
[root@tomcat-web2 ~]# cd /usr/src/
[root@tomcat-web2 src]# mv jdk1.7.0_65/ /usr/local/java
root@tomcat-web2 src]# ln -s /usr/local/java/bin/* /usr/bin/
[root@tomcat-web2 src]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin
[root@tomcat-web2 src]# source /etc/profile.d/java.sh
[root@tomcat-web2 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin
[root@tomcat-web2 src]# 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 web02
root@tomcat-web2 ~]# tar zxvf apache-tomcat-7.0.54.tar.gz -C /usr/src/
[root@tomcat-web2 ~]# cd /usr/src/
[root@tomcat-web2 src]# mv apache-tomcat-7.0.54/ /usr/local/tomcat
[root@tomcat-web2 src]# /usr/local/tomcat/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat-web2 src]# netstat -anput | grep 8080
tcp 0 0 :::8080 :::* LISTEN 20632/java
[root@tomcat-web2 ~]# vim /web/xiaoyu/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 pge</title>
</head>
<body>
<% out.println(“ This is are tomcat website ;http://xiaoyu.test2.com");%>
</body>
</html>
root@tomcat-web2 ~]# vim /usr/local/tomcat/conf/server.xml
tomcatweb01网站页面验证
[root@tomcat-web2 ~]# elinks 192.168.20.163:8080
######################以上步骤,nginx,以及两台tomcat网站已经搭建完成##########################
接下来我们需要让nginx代理两台tomcat网站,并实现负载均衡
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
[验证]
打开本地浏览器输入nginx自己的IP地址,反复刷新,即可出现另外两台tomcat页面,即实现负载
http://192.168.20.194/index.jsp
以上是关于nginx+tomcat负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
Nginx + Tomcat 实现 负载均衡 和 动静分离群集
nginx+2tomcat的负载均衡环境,负载均衡策略用权重形式