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://www.apyobr.com/article/jcpddd.html">http://www.apyobr.com/article/jcpddd.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/doehidi.html">linux执行命令很慢 linux命令卡死不执行</a>
                </li><li>
                    <a href="/article/doehico.html">易优cms标签 易优cms仿站教程</a>
                </li><li>
                    <a href="/article/doeheje.html">c语言写矩阵转置子函数 3*3矩阵转置 c语言函数调用</a>
                </li><li>
                    <a href="/article/doehidp.html">Python三大函数 python函数</a>
                </li><li>
                    <a href="/article/doehepj.html">php如何取数据 php获取数据</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:18982081108" target="_blank">18982081108</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/sosuo.html" target="_blank">网站排名优化</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/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/wangzhandingzhi.html" target="_blank">成都定制网站</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发稿投放</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器机柜租赁</a><a href="https://www.cdcxhl.com/pinpai.html" target="_blank">成都品牌网站建设公司</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文营销</a><a href="https://www.cdcxhl.com/link/" target="_blank">外链</a><a href="https://www.cdcxhl.com/zuyong/" target="_blank">成都服务器租用</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">机柜租用</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音运营公司</a><a href="https://www.cdcxhl.com/app.html" target="_blank">成都app软件开发公司</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发</a><a href="https://www.cdcxhl.com/app.html" target="_blank">app软件开发公司</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://www.kswcd.cn/serve/" target="_blank">专业网站建设</a><a href="http://www.pzhzwz.com/" target="_blank">攀枝花网站建设</a><a href="https://www.cdxwcx.com/city/shifang/" target="_blank">什邡网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="http://www.cdfuwuqi.com/jigui/" target="_blank">成都机柜租用</a><a href="https://www.cdcxhl.com/tuoguan/zongshu/" target="_blank">成都棕树机房</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳服务器托管</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://chengdu.cdcxhl.com/" target="_blank">成都营销网站制作</a><a href="http://seo.cdkjz.cn/wangzhan/" target="_blank">网站制作公司</a><a href="https://www.cdcxhl.com/zhizuo/chengdu.html" 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/shoulu/" target="_blank">分类目录网站</a><a href="https://www.cdcxhl.com/service/service.html" 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="https://www.cdcxhl.com/xiaochengx.html">小程序开发</a>、<a href="https://www.cdcxhl.com/app.html">APP开发</a>、
                        <a href="https://www.cdcxhl.com/yingxiao.html">营销型网站建设</a>、<a href="https://www.cdxwcx.com/">网站建设</a>、
                        <i><a href="https://www.cdcxhl.com/wangzhandingzhi.html">网站定制开发</a></i> ,就选<a href="http://www.apyobr.com/">灿优商贸</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:大悟县灿优商贸有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">鄂ICP备2025125667号-8</a>
                服务热线:18982081108
            </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="#" 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>