使用nginx实现反向代理去负载均衡轮询访问tomcat服务器池

tomcat群集

实验对象:三台centos7服务器

其中nginx负载

nginx 192.168.247.206

tomcat1 192.168.247.160

tomcat2 192.168.247.161

[root@lamp ~]# hostnamectl set-hostname nginx
[root@lamp ~]# su
[root@nginx ~]# 
[root@nginx ~]# hostnamectl set-hostname tomcat1
[root@nginx ~]# su
[root@tomcat1 ~]# 
[root@localhost ~]# hostnamectl set-hostname tomcat2
[root@localhost ~]# su
[root@tomcat2 ~]# 

实验步骤:

1.先处理tomcat

[root@tomcat1 ~]# systemctl stop firewalld
[root@tomcat1 ~]# setenforce 0
[root@tomcat1 ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@tomcat1 ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@tomcat1 ~]# cd /abc
[root@tomcat1 abc]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat1 abc]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
[root@tomcat1 abc]# source /etc/profile
[root@tomcat1 abc]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/
[root@tomcat1 abc]# cd /usr/local
[root@tomcat1 local]# ls
apache-tomcat-8.5.16  etc    include      lib    libexec  sbin   src
bin                   games  jdk1.8.0_91  lib64  nginx    share
[root@tomcat1 local]# mv apache-tomcat-8.5.16/ tomcat
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin

tomcat安装完毕,接下来创建站点

[root@tomcat1 local]# mkdir -pv /web/webapp1
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/webapp1’
[root@tomcat1 local]# cd /web/webapp1/
[root@tomcat1 webapp1]# ls
[root@tomcat1 webapp1]# vim index.jsp
<%@ page language="java" import="java.uitl.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>JSP test1 page</title>
 </head>
 <body>
  <% out.println("Welcome KGC Web");%>
 </body>
</html>

备注:tomcat2里面KGC改为ACCP

添加指定站点

[root@tomcat1 webapp1]# vim /usr/local/tomcat/conf/server.xml 
148       <Host name="localhost"  appBase="webapps"
149             unpackWARs="true" autoDeploy="true">
150 //加入下面两行
151         <Context docBase="/web/webapp1" path="" reloadable="false">
152         </Context>
[root@tomcat1 webapp1]# 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/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

在这里插入图片描述

此时再同样配置tomcat2

验证

在这里插入图片描述

接下来轮到nginx服务器

[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum install gcc-c++ gcc pcre-devel zlib-devel make -y
[root@nginx ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@nginx ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@nginx ~]# cd /abc
[root@nginx abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@nginx abc]# useradd -M -s /sbin/nologin nginx
[root@nginx abc]# cd /opt/nginx-1.12.0/
[root@nginx nginx-1.12.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module

http-flv-module 视频模块

[root@nginx nginx-1.12.0]# make && make install
[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
#gzip  on;
#下面tomcat的服务器池定义在server{}外面
    upstream tomcat_server {
        server 192.168.247.160:8080 weight=1;
        server 192.168.247.161:8080 weight=1;
    }

    server {
。。。
        location / {
            root   html;
            index  index.html index.htm;
            #下面一行是添加内容
            proxy_pass http://tomcat_server;
        }

[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.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 nginx-1.12.0]# nginx
[root@nginx nginx-1.12.0]# netstat -natp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      66405/nginx: master 

访问验证

在这里插入图片描述

刷新一下

在这里插入图片描述

使用nginx实现反向代理去负载均衡轮询访问tomcat服务器池

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值