设为首页 收藏本站
查看: 1004|回复: 0

[经验分享] Nginx+Tomcat 负载均衡群集

[复制链接]

尚未签到

发表于 2017-10-26 11:01:44 | 显示全部楼层 |阅读模式
####实验环境  centos 6.5
####需要的安装包
jdk-7u65-linux-x64.gz
apache-tomcat-7.0.54.tar.gz
nginx-1.6.0.tar.gz

两台tomcat服务器,一台Nginx 服务器
tomcat1 的ip 192.168.1.100:8080
tomcat2 的ip 192.168.1.101:8080
Nginx 的ip 192.168.1.102

注:试验中必须关闭防火墙,selinux和安装了gcc的环境

=========================tomcat1 的配置=============================
39634f5896cf09d80f5ef8a3b69e121c.png-wh_500x0-wm_3-wmp_4-s_1580612593.png

(1)安装JDK,配置JAVA 环境
[iyunv@localhost ~]# tar zxvf jdk-7u65-linux-x64.gz
[iyunv@localhost ~]# mv jdk1.7.0_65/ /usr/local/java
[iyunv@localhost ~]# vim /etc/profile.d/java.sh   #在此路径下写一个脚本,内容如下   
     export JAVA_HOME=/usr/local/java
     export PATH=$PATH:$JAVA_HOME/bin

[iyunv@localhost ~]# source /etc/profile.d/java.sh   
(2)安装配置Tomcat
[iyunv@localhost ~]# tar xf apache-tomcat-7.0.54.tar.gz
[iyunv@localhost ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat7
[iyunv@localhost ~]# /usr/local/tomcat7/bin/startup.sh   #启动tomcat,如下所示
Using CATALINA_BASE:   /usr/local/tomcat7
Using CATALINA_HOME:   /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
You have new mail in /var/spool/mail/root
(3)打开浏览器访问测试,http://  192.168.1.100:8080,出现如下界面则成功

3ebb44cffb48112c8b889986302e9923.png
(4)建立JAVA的WEB 站点
[iyunv@localhost ~]# mkdir -pv /web/webapp1   #创建web页面的目录
[iyunv@localhost ~]# vim /usr/local/tomcat7/conf/server.xml  #修改配置文件,指定路径,添加黄色区域内容
内容如下:
  <Context docBase="/web/webapp1" path="" reloadable="false">
            </Context>
b3e79942765d20965ceca3c1ad9b9d8c.png
[iyunv@localhost ~]# vim /web/webapp1/index.jsp   #添加测试页面
内容如下:
[iyunv@localhost java]# cat /web/webapp1/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>
(5)验证测试成功
打开浏览器,输入http://192.168.1.100:8080,出现如下所示
db1b527b51ffee7fd7cc7a901f454782.png
=========================Tomcat2 的配置=====================

Tomcat2 的配置方法基本同tomcat1,其中包括
(1)关闭iptables 防火墙
(2)安装JDK, 配置JAVA 环境,版本与Tomcat1 server 保持一致
(3)安装配置Tomcat,版本与Tomcat1 server 保持一致
(4)创建/web/webapp1 目录,修改Tomcat配置文件server.xml,将网站文件目录更改到/web/webapp1/路径下
(5)在/web/webapp1/路径下建立Index.jsp,为了区别测试页面index.jsp 的内容更改如下
[iyunv@localhost ~]# cat  /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
  <title>JSP test2 page</title>
</head>
<body>
   <% out.println("welcome to test site,http://www.test2.com");%>
</body>
</html>

==========================Nginx 的配置==============================
[iyunv@localhost 桌面]# yum -y install prce-devel zlib-devel openssl-devel
[iyunv@localhost 桌面]# groupadd www
[iyunv@localhost 桌面]# useradd -g www www -s /bin/false
root@localhost 桌面]# mv nginx-1.6.0.tar.gz ~
[iyunv@localhost 桌面]# cd ~
[iyunv@localhost ~]# tar zxvf nginx-1.6.0.tar.gz
[iyunv@localhost ~]# cd nginx-1.6.0
[iyunv@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
[iyunv@localhost nginx-1.6.0]# make && make install
安装完成后编辑配置文件(前面为配置文件的行数,红色部分为添加内容)
[iyunv@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf

32     #gzip  on;
33    upstream tomcat_server {
34         server 192.168.1.100:8080 weight=1;
35         server 192.168.1.101:8080 weight=2;
36
37             }


43
44         #access_log  logs/host.access.log  main;
45
46         location / {
47             root   html;
48             index  index.html index.htm;
49             proxy_pass   http://tomcat_server;
50         }
51
52         #error_page  404              /404.html;

为了启动方便写一个Nginx 的脚本,并把它放在环境变量中,脚本内容如下
[iyunv@localhost init.d]# vim /etc/init.d/nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

#chkconfig: - 99 20
#description: Nginx Server Control Script

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
  $PROG
  ;;
  stop)
   kill -s QUIT $(cat $PIDF)
  ;;
  restart)
   $0 stop
   $0 start
  ;;
  reload)
  kill -s HUP $(cat $PIDF)
  ;;
  *)
   echo "Usage: $0 (start|stop|restart|reload)"
   exit 1
esac
exit 0



[iyunv@localhost init.d]# echo "PATH=$PATH:/etc/init.d/" >> /etc/profile
[iyunv@localhost init.d]# . /etc/profile
[iyunv@localhost init.d]# nginx restart     #重启Nginx
测试配置文件是否正确
[iyunv@localhost init.d]# /usr/local/nginx/sbin/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

重新启动nginx
[iyunv@localhost init.d]# nginx restart
[iyunv@localhost init.d]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      40041/nginx
测试负载均衡效果
打开浏览器输入http://192.168.1.102
不断刷新浏览器测试发现权重相同,页面会反复在两个页面来回切换。


注:如果是虚拟机调到同一个wmnat中,且配置ip地址是永久的,尽量不要配临时的容易出错

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-405959-1-1.html 上篇帖子: 基于corosync+pacemaker实现nfs+nginx部署 下篇帖子: Nginx启动脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表