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

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实现负载均衡_第1张图片

二、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'

         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    
    
[root@tomcat ~]# 
[root@tomcat ~]# cat tomcat_8081/conf/server.xml |grep -E '80|90'

         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    
    
[root@tomcat ~]# 
[root@tomcat ~]# cat tomcat_8082/conf/server.xml |grep -E '80|90'

         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    
    

nginx反向代理同一台主机多个tomcat实现负载均衡_第2张图片

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

        
                 test

        

        
        SessionID is <%=session.getId()%>
        
SessionIP is <%=request.getServerName()%>
SessionPort is <%=request.getServerPort()%>

tomcat_8080

三、客户端访问

1、查看

nginx反向代理同一台主机多个tomcat实现负载均衡_第3张图片nginx反向代理同一台主机多个tomcat实现负载均衡_第4张图片

nginx反向代理同一台主机多个tomcat实现负载均衡_第5张图片

你可能感兴趣的:(Web架构实战)