本文在本地虚拟机进行测试演示
虚拟机环境:CentOS 7.6.1810
Linux内核:3.10.0-957.l7 x86_64

1. 必要环境

java 运行环境 本文安装 orcale java 1.8
nginx web服务器
tomcat web服务器

2. 部署tomcat服务

本文使用本地环境运行了三台tomcat , 分别是 8001, 8002, 8003

  1. [root@kernhome:10:47 conf]#netstat -tnlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. LISTEN 6809/nginx: master
  5. t LISTEN 9695/java
  6. tcp6 0 0 :::8001 :::* LISTEN 9585/java
  7. tcp6 0 0 :::8002 :::* LISTEN 9640/java
  8. tcp6 0 0 :::8003 :::* LISTEN 9695/java

稍微修改tomcat首页,效果如下图
http://192.168.61.135:8001
image.png
http://192.168.61.135:8002
image.png
http://192.168.61.135:8003
image.png

3. Nginx负载均衡服务配置

编写nginx tomcat集群的配置文件

  1. [root@kernhome:10:48 conf]#vi /data/web/nginx.conf/tomcats.conf
  1. #定义负载均衡上游服务器,weight 配置权重
  2. upstream tomcats {
  3. server localhost:8001 weight=5;
  4. server localhost:8002 weight=3;
  5. server localhost:8003 weight=1;
  6. }
  7. #监听80端口,并做tomcats集群服务的反向代理
  8. server {
  9. listen 80;
  10. server_name localhost;
  11. # 定义代理服务器的路由
  12. location / {
  13. #代理到上游服务器tomcats
  14. proxy_pass http://tomcats;
  15. }
  16. }

将配置文件引入到nginx主配置文件中

  1. [root@kernhome:10:57 conf]#vi /usr/local/nginx/conf/nginx.conf
  1. http {
  2. # 在http块中引入/data/web/nginx.conf/目录下所有以.conf为后缀的配置文件
  3. include /data/web/nginx.conf/*.conf
  4. }

重启nginx

  1. [root@kernhome:11:00 conf]#nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@kernhome:11:00 conf]#nginx -s reload

4.访问测试

第一次访问,
image.png
第二次访问,
image.png
第三次访问
image.png