Nginx + Tomcat 实现负载均衡 详细示例

1、实验拓扑

在这里插入图片描述

2、各个虚机的软件安装

2.1 Nginx Server

2.1.1 Nginx 安装

[root@Tang-7 ~]# ipinfo
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.10  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-7 ~]# yum install nginx -y

2.1.2 Nginx 启动

[root@Tang-7 ~]# systemctl start nginx.service
[root@Tang-7 ~]# ss -tnl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:80                                                                            *:*                  
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      128                                                              :::80                                                                           :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*                   

2.1.3 Nginx 访问

在这里插入图片描述

2.2 Tomcat - 2

2.2.1 Tomcat 安装

[root@Tang-6 ~]# ipinfo
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-6 ~]# yum list all tomcat*
Installed Packages
tomcat.noarch                                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-admin-webapps.noarch                                                                       7.0.76-9.el7_6                                                                      @base
tomcat-docs-webapp.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-el-2.2-api.noarch                                                                          7.0.76-9.el7_6                                                                      @base
tomcat-jsp-2.2-api.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-lib.noarch                                                                                 7.0.76-9.el7_6                                                                      @base
tomcat-servlet-3.0-api.noarch                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-webapps.noarch                                                                             7.0.76-9.el7_6                                                                      @base
Available Packages
tomcat-javadoc.noarch                                                                             7.0.76-9.el7_6                                                                      base 
tomcat-jsvc.noarch                                                                                7.0.76-9.el7_6                                                                      base 
tomcat-native.x86_64                                                                              1.2.21-1.el7                                                                        epel 
tomcatjss.noarch                                                                                  7.2.1-8.el7_6                                                                       base 
[root@Tang-6 ~]# yum install tomcat.noarch tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-webapps.noarch -y

2.2.2 Tomcat 启动

[root@Tang-6 ~]# systemctl start tomcat.service
[root@Tang-6 ~]# ss -tnl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      100                                                              :::8009                                                                         :::*                  
LISTEN      0      100                                                              :::8080                                                                         :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*                  
LISTEN      0      1                                                  ::ffff:127.0.0.1:8005                                                                         :::*                  

2.2.3 Tomcat 访问

在这里插入图片描述

2.3 Tomcat - 1

2.3.1 Tomcat 安装

[root@Tang-5 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-5 ~]# yum list all tomcat*
Installed Packages
tomcat.noarch                                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-admin-webapps.noarch                                                                       7.0.76-9.el7_6                                                                      @base
tomcat-docs-webapp.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-el-2.2-api.noarch                                                                          7.0.76-9.el7_6                                                                      @base
tomcat-jsp-2.2-api.noarch                                                                         7.0.76-9.el7_6                                                                      @base
tomcat-lib.noarch                                                                                 7.0.76-9.el7_6                                                                      @base
tomcat-servlet-3.0-api.noarch                                                                     7.0.76-9.el7_6                                                                      @base
tomcat-webapps.noarch                                                                             7.0.76-9.el7_6                                                                      @base
Available Packages
tomcat-javadoc.noarch                                                                             7.0.76-9.el7_6                                                                      base 
tomcat-jsvc.noarch                                                                                7.0.76-9.el7_6                                                                      base 
tomcat-native.x86_64                                                                              1.2.21-1.el7                                                                        epel 
tomcatjss.noarch                                                                                  7.2.1-8.el7_6                                                                       base 
[root@Tang-5 ~]# yum install tomcat.noarch tomcat-admin-webapps.noarch tomcat-docs-webapp.noarch tomcat-webapps.noarch -y

2.3.2 Tomcat 启动

[root@Tang-5 ~]# systemctl start tomcat.service
[root@Tang-5 ~]# ss -tnl
State       Recv-Q Send-Q                                                Local Address:Port                                                               Peer Address:Port              
LISTEN      0      128                                                               *:22                                                                            *:*                  
LISTEN      0      100                                                       127.0.0.1:25                                                                            *:*                  
LISTEN      0      100                                                              :::8009                                                                         :::*                  
LISTEN      0      100                                                              :::8080                                                                         :::*                  
LISTEN      0      128                                                              :::22                                                                           :::*                  
LISTEN      0      100                                                             ::1:25                                                                           :::*                  
LISTEN      0      1                                                  ::ffff:127.0.0.1:8005                                                                         :::*                  

2.3.3 Tomcat 访问

在这里插入图片描述

3、Tomcat 冷部署一个测试类应用

3.1 Tomcat - 1

3.1.1 创建相关目录

[root@Tang-6 ~]# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
mkdir: created directory ‘/appdata’
mkdir: created directory ‘/appdata/webapps’
mkdir: created directory ‘/appdata/webapps/ROOT’
mkdir: created directory ‘/appdata/webapps/ROOT/lib’
mkdir: created directory ‘/appdata/webapps/ROOT/classes’
mkdir: created directory ‘/appdata/webapps/ROOT/WEB-INF[root@Tang-6 ~]# mkdir /appdata/webapps/test

3.1.2 创建相关文件

[root@Tang-6 ~]# cat /appdata/webapps/test/index.jsp
<%@ page language="java" %>
<html>
	<head><title>TomcatA</title></head>
		<body>
		<h1><font color="red">Tang-6</font></h1>
		<table align="centre" border="1">
			<tr>
				<td>Session ID</td>
			<% session.setAttribute("Tang","Tang"); %>
				<td><%= session.getId() %></td>
			</tr>
			<tr>
				<td>Created on</td>
				<td><%= session.getCreationTime() %></td>
			</tr>
		</table>
	</body>
</html>
[root@Tang-6 ~]# tree /appdata/webapps/
/appdata/webapps/
├── ROOT
│   ├── classes
│   ├── lib
│   └── WEB-INF
└── test
    └── index.jsp

5 directories, 1 file

3.1.3 修改 Tomcat 配置文件,并重启服务

### 在 </Engine> </Engine> 之间添加一个 HOST ###
[root@Tang-6 ~]# vim /etc/tomcat/server.xml 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat-Tang-6">
	<Host name="localhost" appBase="/appdata/webapps" unpackWARs="true" autoDeploy="true">
    	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    	prefix="node1_access" suffix=".log"
        pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="/test" docBase="test" reloadable="">
                <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                prefix="Tang-6_test_access_" suffix=".log"
                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Context>
	</Host>
</Engine>
[root@Tang-6 ~]# systemctl restart tomcat.service

3.1.4 修改 WIN7 HOSTS 文件,进行访问,并查看相关 log

在这里插入图片描述
在这里插入图片描述

3.1.5 查看相关 log

[root@Tang-6 ~]# tail /var/log/tomcat/Tang-6_test_access_2019-11-03.log 
192.168.1.6 - - [03/Nov/2019:20:42:11 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:15 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:20:42:16 +0800] "GET /test/ HTTP/1.1" 200 321

3.2 Tomcat - 2

3.2.1 创建相关目录

[root@Tang-5 ~]# mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
mkdir: created directory ‘/appdata’
mkdir: created directory ‘/appdata/webapps’
mkdir: created directory ‘/appdata/webapps/ROOT’
mkdir: created directory ‘/appdata/webapps/ROOT/lib’
mkdir: created directory ‘/appdata/webapps/ROOT/classes’
mkdir: created directory ‘/appdata/webapps/ROOT/WEB-INF[root@Tang-5 ~]# mkdir /appdata/webapps/test

3.2.2 创建相关文件

[root@Tang-5 ~]# cat /appdata/webapps/test/index.jsp 
<%@ page language="java" %>
<html>
	<head><title>TomcatB</title></head>
		<body>
		<h1><font color="blue">Tang-5</font></h1>
		<table align="centre" border="1">
			<tr>
				<td>Session ID</td>
			<% session.setAttribute("Tang","Tang"); %>
				<td><%= session.getId() %></td>
			</tr>
			<tr>
				<td>Created on</td>
				<td><%= session.getCreationTime() %></td>
			</tr>
		</table>
	</body>
</html>	
[root@Tang-5 ~]# tree /appdata/webapps/
/appdata/webapps/
├── ROOT
│   ├── classes
│   ├── index.jsp
│   ├── lib
│   └── WEB-INF
└── test
    └── index.jsp

5 directories, 2 files

3.2.3 修改 Tomcat 配置文件,并重启服务

### 在 </Engine> </Engine> 之间添加一个 HOST ###
[root@Tang-5 ~]# vim /etc/tomcat/server.xml 
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat-Tang-5">			
	<Host name="localhost" appBase="/appdata/webapps" unpackWARs="true" autoDeploy="true">
    	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="node1_access" suffix=".log"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="/test" docBase="test" reloadable="">
        	<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
             prefix="Tang-5_access_" suffix=".log"
             pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        </Context>
	</Host>
<Engine>
[root@Tang-5 ~]# systemctl restart tomcat.service

3.2.4 修改 WIN7 HOSTS 文件,进行访问

在这里插入图片描述
在这里插入图片描述

3.2.5 查看相关 log

[root@Tang-5 ~]# tail /var/log/tomcat/Tang-5_test_access_2019-11-03.log 
192.168.1.6 - - [03/Nov/2019:20:42:09 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:13 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:13 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:20:42:14 +0800] "GET /test/ HTTP/1.1" 200 322

4、Nginx Server 反代设置

4.1 conf 文件修改

[root@Tang-7 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.10  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-7 ~]# vim /etc/nginx/nginx.conf
http {

    upstream tomcatservers {
        server 192.168.1.11:8080;
        server 192.168.1.12:8080;
    }

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        index index.jsp index.html;

        location / {
            root /usr/share/nginx/html;
        }

        location /test/ {
            proxy_pass http://tomcatservers;
        }
        
    }
}

4.2 conf 文件语法检查,并重启服务

[root@Tang-7 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@Tang-7 ~]# nginx -s reload

4.3 进行访问,通过刷新网页,可以看到在轮询访问后端 Tomcat 服务器

在这里插入图片描述
在这里插入图片描述

4.4 后端 Tomcat 服务器 log 查看

[root@Tang-6 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-6 ~]# tail /var/log/tomcat/Tang-6_test_access_2019-11-03.log 
192.168.1.10 - - [03/Nov/2019:21:19:19 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:19:34 +0800] "GET /test HTTP/1.1" 302 -
192.168.1.6 - - [03/Nov/2019:21:19:34 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:28:31 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:28:31 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:28:31 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:28:32 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.10 - - [03/Nov/2019:21:30:35 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.6 - - [03/Nov/2019:21:48:50 +0800] "GET /test/ HTTP/1.1" 200 321
192.168.1.10 - - [03/Nov/2019:21:54:35 +0800] "GET /test/ HTTP/1.1" 200 321
[root@Tang-5 ~]# ipinfo 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.12  netmask 255.255.255.0  broadcast 192.168.1.255
--
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
[root@Tang-5 ~]# tail /var/log/tomcat/Tang-5_test_access_2019-11-03.log 
192.168.1.10 - - [03/Nov/2019:21:29:44 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.10 - - [03/Nov/2019:21:30:21 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:21:31:29 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:21:31:30 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:21:48:52 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:21:48:52 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.6 - - [03/Nov/2019:21:48:52 +0800] "GET /test/ HTTP/1.1" 200 322
192.168.1.10 - - [03/Nov/2019:21:56:34 +0800] "GET /test/ HTTP/1.0" 200 322
192.168.1.10 - - [03/Nov/2019:22:02:32 +0800] "GET /test/ HTTP/1.0" 200 322
192.168.1.10 - - [03/Nov/2019:22:06:11 +0800] "GET /test/ HTTP/1.0" 200 322

5、注意事项(反代其它主机名失败的原因)

进行反代时,只能反代到后端 Tomcat 服务器的 localhost 上,如果反代到其它 主机名 的话,反代失败。提示 404 。

发布了158 篇原创文章 · 获赞 7 · 访问量 9764

猜你喜欢

转载自blog.csdn.net/weixin_44983653/article/details/102888211