Linux下Nginx负载均衡多个tomcat配置的方法步骤-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Linux下Nginx负载均衡多个tomcat配置的方法步骤

Linux下安装nginx和安装多个tomcat的方法这里不过多介绍,不清楚的可参考:

在岱岳等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、成都网站制作 网站设计制作按需定制制作,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,外贸网站建设,岱岳网站建设费用合理。

Linux安装nginx:

https://www.jb51.net/article/159519.htm

Linux安装多个tomcat:

https://www.jb51.net/article/159521.htm

当我们服务器安装好了nginx,并且安装了多台tomcat,那么我们现在可以试着玩玩nginx的负载均衡

先简单介绍我的运行环境

一台阿里云服务器

Linux系统,jdk1.8, 已安装好nginx,

安装了4个tomcat,并且配置好了各端口号,分别对应8080,8081,8082,8083

Linux下Nginx负载均衡多个tomcat配置的方法步骤

一:进入nginx目录下的conf目录 

这是我的nginx安装目录:   

[root@aliServer ~]# cd /usr/local/nginx/conf

二:编辑nginx.conf

Linux下Nginx负载均衡多个tomcat配置的方法步骤

[root@aliServer conf]# vi nginx.conf

三:配置服务器组

1:在http{}节点之间添加upstream配置。(注意不要写localhost,不然访问速度会很慢)

upstream nginxDemo {
  server 127.0.0.1:8081;  #服务器地址1
  server 127.0.0.1:8082;  #服务器地址2
  server 127.0.0.1:8082;  #服务器地址3
  server 127.0.0.1:8083;  #服务器地址4
}

2:修改nginx监听的端口号80

nginx默认端口是80,这里我暂未更改,保持不变

server {
  listen    80;   #默认是80,也可更改为其他的,当然已被占用的端口号不能写。
  ......
}

3:用proxy_pass配置反向代理地址

在location\{}中,利;此处“http://”不能少,后面的地址要和第一步upstream定义的名称保持一致(也就是nginxDemo这个名称是自定义的,两个地方需要一致)

location / {
      root  html;
      index index.html index.htm;
      proxy_pass http://nginxDemo; #配置方向代理地址
    }

配置完成后,如图:

Linux下Nginx负载均衡多个tomcat配置的方法步骤

四:启动nginx

我的安装nginx路径是  /usr/local/nginx

所以我的启动命令是:

[root@aliServer ~]# /usr/local/nginx/sbin/nginx

因为nginx之前安装时就已经启动了,现在再启动就报错端口号被占用

Linux下Nginx负载均衡多个tomcat配置的方法步骤

这时我们使用命令查看各端口号占用情况

[root@aliServer ~]# netstat -ntpl

Linux下Nginx负载均衡多个tomcat配置的方法步骤

我们看到,nginx被9097这个PID占用着,使用kill -9杀掉

[root@aliServer ~]# kill -9 9097

再次启动nginx

[root@aliServer ~]# /usr/local/nginx/sbin/nginx

没有任何反应,这就对了,这时在浏览器中输入你服务器地址

Linux下Nginx负载均衡多个tomcat配置的方法步骤

说明nginx启动成功,至于配置是否正确,是否能负载匀衡,现在开始验证。。。

五:验证

我们都知道,nginx负载均衡时客户端所有请求都经过nginx,那么nginx就可以决定将这些请求转发给谁,如果服务器A的资源更充分(CPU更多、内存更大等等),服务器B没有服务器A处理能力强,那么nginx就会把更多的请求转发到A,转发较少的请求到服务器B,这样就做到了负载均衡,而且就算其中一台服务器宕机了,对于用户而言也能正常访问网站。

在验证前,需要先做点准备。

1:准备一个简单点的jsp,如:

Linux下Nginx负载均衡多个tomcat配置的方法步骤

我一台服务器上安装了4个tomcat,所以我准备了4个index.jsp文件

分别是

Tomcat8080<title> <h2>Hellow Tomcat_8080</h2>
<title>Tomcat8081<title> <h2>Hellow Tomcat_8081</h2>
<title>Tomcat8082<title> <h2>Hellow Tomcat_8082</h2>
<title>Tomcat8083<title> <h2>Hellow Tomcat_8083</h2></pre></div><p>这里需要注意的是:jsp文件的名字一定要是index.jsp,因为tomcat启动成功的画面,如图:</p><p><img src="/upload/otherpic60/56444.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p>读取就是tomcat安装目录下的webapps/ROOT/index.jsp</p><p>我的地址是:/usr/java/tomcat/tomcat_8080/webapps/ROOT</p><p><img src="/upload/otherpic60/56445.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p>将先前准备的4个index.jsp文件,覆盖每个tomcat默认的index.jsp文件。</p><p>启个各个tomcat</p><div><pre>[root@aliServer bin]# ./startup.sh</pre></div><p>这时我们再在浏览器输入 xxx.xxx.xx.xx:8080  你会发现,不在出现那只小猫了,而是。。。。。。</p><p><img src="/upload/otherpic60/56446.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p><img src="/upload/otherpic60/56447.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p><img src="/upload/otherpic60/56448.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p><img src="/upload/otherpic60/56449.png" alt="Linux下Nginx负载均衡多个tomcat配置的方法步骤"></p><p>4个tomcat都启动成功了,nginx也已启动成功了。</p><p>这时输入在浏览器输入你的服务器ip,不停的刷新页面,你会发现页面一会显示8080,一会显示8081,一会显示8082,一会显示8083,当然这个是nginx根据哪个服务器资源更充分而决定请求去什么地方的,我们在浏览器的请求的地址不变,却访问的是不同的tomcat服务器,说明,nginx配置成功。</p><p>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。</p>            
            
                        <br>
            网页标题:Linux下Nginx负载均衡多个tomcat配置的方法步骤            <br>
            当前路径:<a href="http://kswsj.cn/article/jcpddd.html">http://kswsj.cn/article/jcpddd.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dsghepo.html">路由器pvc 路由器pvc怎么设置</a>
                </li><li>
                    <a href="/article/dsghepg.html">阿里云服务器可用区怎么选 阿里云可用区选什么</a>
                </li><li>
                    <a href="/article/dsghegj.html">php实时查询数据库 php查询数据库内容表格</a>
                </li><li>
                    <a href="/article/dsghegp.html">go语言区别 go 语言 gui</a>
                </li><li>
                    <a href="/article/dsghego.html">sap系统怎么查订单号的简单介绍</a>
                </li>        </ul>
    </div>
</div>
<div class="line"></div>
<!--底部-->
<footer id="5">
    <div class="foot1 container">
        <div class="list">
            <div class="item">
                <a href="javascript:;">
                    <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span>
                    <strong>关注我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>索要报价</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>我要咨询</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>找到我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>投诉建议</strong>
                </a>
            </div>
        </div>
        <div class="tel">
            <dl>
                <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br>
                <span>也许您需要专业的服务,欢迎来电咨询</span>
            </dl>
            <dl>
                <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br>
                <span>您的需求,是我们前进的动力</span>
            </dl>
        </div>
    </div>
    <div class="friend">
        <div class="container">
            <span class="tit">友情链接:</span>
            <div class="inner">
                <a href="https://www.cdcxhl.com/yunying.html" target="_blank">网站运营</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">品牌网站建设</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站设计</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">成都品牌网站建设公司</a><a href="https://www.cdcxhl.com/shoulu/" target="_blank">网站收录</a><a href="https://www.cdcxhl.com/zuyong/" target="_blank">成都租用服务器</a><a href="https://www.cdcxhl.com/app.html" target="_blank">成都app软件开发公司</a><a href="https://www.cdcxhl.com/" target="_blank">建站公司</a><a href="https://www.cdcxhl.com/shop.html" target="_blank">商城网站建设</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音运营公司</a><a href="https://www.cdcxhl.com/gaiban/" target="_blank">网站改版公司</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">成都抖音运营</a><a href="https://www.cdcxhl.com/waimao.html" target="_blank">外贸建站</a><a href="https://www.cdcxhl.com/gaiban/chengdu.html" target="_blank">成都网站改版</a><a href="https://www.cdcxhl.com/yingxiao.html" target="_blank">营销型网站建设</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租用</a><a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">西云移动机房</a><a href="https://www.cdcxhl.com/" target="_blank">网站制作公司</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://www.cdkjz.cn/fangan/education/" target="_blank">教育培训网站建设方案</a><a href="http://m.cdcxhl.cn/seo/" target="_blank">营销网站建设</a><a href="https://www.cdcxhl.com/h5.html" target="_blank">成都h5网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdcxhl.com/idc/yaan.html" target="_blank">雅安服务器托管</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳服务器托管</a><a href="https://www.cdcxhl.com/idc/mianyang.html" target="_blank">绵阳服务器托管</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://chengdu.cdxwcx.cn/wangzhan/" target="_blank">手机网站制作</a><a href="http://m.cdcxhl.cn/mobile/" target="_blank">手机网站制作</a><a href="http://www.36103.cn/" target="_blank">成都网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/shoulu/" target="_blank">免费收录网站</a><a href="https://www.cdcxhl.com/link/" target="_blank">卖友情链接</a><a href="https://www.cdcxhl.com/ruanwen/yingxiao/" target="_blank">软文发布平台</a>            </div>
            <div class="fr ecode">
                <div class="fl">
                    <img src="/Public/Home/img/ewm.jpg">
                    <p>关注企业微信</p>
                </div>
                <div class="fr slogan">
                    <p class="icon">
                        <a class="ph" href=""><i class="iconfont"></i></a>
                        <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a>
                    </p>
                    <p>
                        <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、
                        <a href="">营销型网站建设</a>、<a href="">网站建设</a>、
                        <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:成都创新互联科技有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a>
                服务热线:028-86922220
            </p>
            <p class="fr">
                <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:
                <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a>
            </p>
        </div>
    </div>
</footer>
<!--在线咨询-->
<div class="fot">
    <ul>
        <li>
            <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank">
                <img src="/Public/Home/img/fot1.png" alt="">
                <p>在线咨询</p>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" target="_blank">
                <img src="/Public/Home/img/fot2.png" alt="">
                <p>拨打电话</p>
            </a>
        </li>
    </ul>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>