nginx反向代理同一台主机多个tomcat实现负载均衡

导读:本篇文章讲解 nginx反向代理同一台主机多个tomcat实现负载均衡,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

IP规划:

nginx反向代理服务器:192.168.14.40

tomcat1:192.168.14.13:8080

tomcat2:192.168.14.13:8081

tomcat3:192.168.14.13:8082

一、nginx反向代理

1、安装过程查看其它文章操作

nginx反向代理

2、修改配置文件

nginx反向代理同一台主机多个tomcat实现负载均衡

二、tomcat安装

1、到tomcat官网https://tomcat.apache.org/查看下载路径,使用wget下载

[root@tomcat ~]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.24/bin/apache-tomcat-9.0.24.tar.gz

2、解压并重命名

[root@tomcat ~]# tar -zxvf apache-tomcat-9.0.24.tar.gz
[root@tomcat ~]# mv apache-tomcat-9.0.24 tomcat_8080
[root@tomcat ~]# cp tomcat_8080 tomcat_8081
[root@tomcat ~]# cp tomcat_8080 tomcat_8082

3、yum安装tomcat需要的jdk,就不用再手动配置环境变量

[root@tomcat ~]# yum install -y java-1.8*

4、修改配置文件,为多个tomcat分配不同的端口号(默认8080不修改)

[root@tomcat ~]# vim tomcat_8080/conf/server.xml 
[root@tomcat ~]# vim tomcat_8081/conf/server.xml 
[root@tomcat ~]# vim tomcat_8082/conf/server.xml 

[root@tomcat ~]# cat tomcat_8080/conf/server.xml |grep -E '80'
<Server port="8005" shutdown="SHUTDOWN">
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    <Connector port="8080" protocol="HTTP/1.1"
               port="8080" protocol="HTTP/1.1"
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
[root@tomcat ~]# 
[root@tomcat ~]# cat tomcat_8081/conf/server.xml |grep -E '80|90'
<Server port="8091" shutdown="SHUTDOWN">
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    <Connector port="8081" protocol="HTTP/1.1"
               port="8080" protocol="HTTP/1.1"
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9001" protocol="AJP/1.3" redirectPort="8443" />
[root@tomcat ~]# 
[root@tomcat ~]# cat tomcat_8082/conf/server.xml |grep -E '80|90'
<Server port="8092" shutdown="SHUTDOWN">
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    <Connector port="8082" protocol="HTTP/1.1"
               port="8080" protocol="HTTP/1.1"
    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="9002" protocol="AJP/1.3" redirectPort="8443" />

nginx反向代理同一台主机多个tomcat实现负载均衡

5、启动tomcat

[root@tomcat ~]# tomcat_8080/bin/startup.sh
[root@tomcat ~]# tomcat_8081/bin/startup.sh
[root@tomcat ~]# tomcat_8082/bin/startup.sh

查看端口

[root@tomcat ~]# ss -ntlp
State       Recv-Q Send-Q                                         Local Address:Port                                                        Peer Address:Port              
LISTEN      0      128                                                        *:22                                                                     *:*                   users:(("sshd",pid=4886,fd=3))
LISTEN      0      100                                                127.0.0.1:25                                                                     *:*                   users:(("master",pid=5138,fd=13))
LISTEN      0      100                                                       :::9001                                                                  :::*                   users:(("java",pid=15263,fd=60))
LISTEN      0      100                                                       :::8009                                                                  :::*                   users:(("java",pid=15171,fd=60))
LISTEN      0      100                                                       :::9002                                                                  :::*                   users:(("java",pid=15423,fd=60))
LISTEN      0      100                                                       :::8080                                                                  :::*                   users:(("java",pid=15171,fd=55))
LISTEN      0      100                                                       :::8081                                                                  :::*                   users:(("java",pid=15263,fd=55))
LISTEN      0      100                                                       :::8082                                                                  :::*                   users:(("java",pid=15423,fd=55))
LISTEN      0      128                                                       :::22                                                                    :::*                   users:(("sshd",pid=4886,fd=4))
LISTEN      0      100                                                      ::1:25                                                                    :::*                   users:(("master",pid=5138,fd=14))
LISTEN      0      1                                           ::ffff:127.0.0.1:8091                                                                  :::*                   users:(("java",pid=15263,fd=75))
LISTEN      0      1                                           ::ffff:127.0.0.1:8092                                                                  :::*                   users:(("java",pid=15423,fd=75))
LISTEN      0      1                                           ::ffff:127.0.0.1:8005                                                                  :::*                   users:(("java",pid=15171,fd=74))

6、为了便于区分我们修改tomcat里index.jsp里的内容如下

[root@tomcat ~]# vim tomcat_8080/webapps/ROOT/index.jsp
[root@tomcat ~]# vim tomcat_8081/webapps/ROOT/index.jsp
[root@tomcat ~]# vim tomcat_8082/webapps/ROOT/index.jsp

[root@tomcat ~]# cat tomcat_8080/webapps/ROOT/index.jsp
<html>
        <head>
                <title> test</title>

        </head>

        <body>
        SessionID is <%=session.getId()%>
        <br/>

        SessionIP is <%=request.getServerName()%>
        <br/>

        SessionPort is <%=request.getServerPort()%>
		<!-- 端口号依次修改为:8080/8081/8082-->
        <h1>tomcat_8080</h1>

</body>

</html>

三、客户端访问

1、查看

nginx反向代理同一台主机多个tomcat实现负载均衡nginx反向代理同一台主机多个tomcat实现负载均衡

nginx反向代理同一台主机多个tomcat实现负载均衡

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/95282.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!