Nginx + Tomcat 负载均衡搭建
目录
一、原理与理论
二、背景与目的
三、搭建步骤(实操详解)
环境准备
步骤1:Tomcat集群配置
1.1 解压并重命名Tomcat(两台服务器操作)
1.2 修改端口避免冲突(关键!)
节点1配置 (/opt/tomcat1/conf/server.xml):
节点2配置 (/opt/tomcat2/conf/server.xml):
步骤2:Nginx负载均衡配置
2.1 安装Nginx(代理服务器操作)
2.2 配置负载均衡策略
2.3 重启Nginx并检查配置
步骤3:验证负载均衡效果
3.1 测试请求分发
3.2 模拟节点故障
3.3 查看Nginx节点状态
步骤4:Session共享解决方案(附加)
4.1 安装Redis
4.2 配置Tomcat Session管理器 4.2 配置 Tomcat Session 管理器
四、常见问题与解决方案
五、总结与心得
核心价值:
优化方向:
踩坑提醒:
一、原理与理论
核心机制:
通过 Nginx 作为反向代理服务器,将用户请求动态分发到多个 Tomcat 应用服务器,实现流量分摊、避免单点故障。
关键技术点:
-
负载均衡算法:
-
轮询(Round Robin)
:默认方式,按顺序分配请求。 -
加权轮询(Weighted Round Robin)
:根据服务器性能分配权重。 -
IP_Hash
:同一客户端IP固定访问某台Tomcat,解决Session共享问题。
-
-
健康检查:
Nginx 自动检测后端Tomcat状态,故障节点自动剔除。 -
动静分离: 动静分离 :
静态资源(如图片/CSS)由Nginx直接处理,动态请求转发至Tomcat,提升整体性能。
二、背景与目的
为什么需要负载均衡?
-
单台Tomcat并发能力有限(默认线程数约150-200),高并发场景下易崩溃。
-
业务需要无缝升级(滚动发布)、提高系统可用性(故障自动转移)。
-
横向扩展服务器集群,提升系统吞吐量。
典型场景:
电商大促、秒杀活动、企业级应用高可用架构。
三、搭建步骤(实操详解)
环境准备
角色 | IP/域名 | 端口 | 说明 |
---|---|---|---|
Nginx 反向代理 | 192.168.1.10 | 80 | 负责请求分发 |
Tomcat节点1 Tomcat 节点 1 | 192.168.1.11 | 8080 | 应用服务器1 |
Tomcat节点2 Tomcat 节点 2 | 192.168.1.12 | 9090 | 应用服务器2(端口不同) |
步骤1:Tomcat集群配置
1.1 解压并重命名Tomcat(两台服务器操作)
# 节点1操作
tar -zxvf apache-tomcat-9.0.85.tar.gz
mv apache-tomcat-9.0.85 /opt/tomcat1# 节点2操作
tar -zxvf apache-tomcat-9.0.85.tar.gz
mv apache-tomcat-9.0.85 /opt/tomcat2
1.2 修改端口避免冲突(关键!)
-
节点1配置 (
/opt/tomcat1/conf/server.xml
):
<!-- 关闭端口改为8005(默认) -->
<Server port="8005" shutdown="SHUTDOWN"> <!-- HTTP连接器改为8080 -->
<Connector port="8080" protocol="HTTP/1.1" /><!-- AJP端口改为8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
-
节点2配置 (
/opt/tomcat2/conf/server.xml
):
<!-- 关闭端口改为9005(避免冲突) -->
<Server port="9005" shutdown="SHUTDOWN"> <!-- HTTP连接器改为9090 -->
<Connector port="9090" protocol="HTTP/1.1" /><!-- AJP端口改为9009 -->
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
-
1.3 添加测试页面(验证负载均衡)
# 在节点1创建测试页面
echo "Tomcat Server 1" > /opt/tomcat1/webapps/ROOT/index.jsp# 在节点2创建测试页面
echo "Tomcat Server 2" > /opt/tomcat2/webapps/ROOT/index.jsp
- 1.4 启动Tomcat并验证
# 启动服务
/opt/tomcat1/bin/startup.sh
/opt/tomcat2/bin/startup.sh# 检查是否启动成功(节点1)
curl http://192.168.1.11:8080
# 应返回 "Tomcat Server 1"# 检查节点2
curl http://192.168.1.12:9090
# 应返回 "Tomcat Server 2"
步骤2:Nginx负载均衡配置
2.1 安装Nginx(代理服务器操作)
# Ubuntu/Debian
sudo apt update
sudo apt install nginx -y# CentOS
sudo yum install epel-release -y
sudo yum install nginx -y
2.2 配置负载均衡策略
编辑配置文件 /etc/nginx/nginx.conf
,在 http{}
块内添加:
# 定义Tomcat集群
upstream tomcat_cluster {# 使用加权轮询(weight=3的节点接收更多请求)server 192.168.1.11:8080 weight=3; server 192.168.1.12:9090 weight=2;# 健康检查参数(重要!)max_fails=3; # 连续失败3次标记为不可用fail_timeout=10s; # 10秒后重新尝试连接
}server {listen 80;server_name loadbalance-test.com; # 替换为你的域名或IP# 动态请求转发至Tomcat集群location / {proxy_pass http://tomcat_cluster;# 传递客户端真实信息proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 连接超时设置(避免502错误)proxy_connect_timeout 60s;proxy_read_timeout 600s;}# 静态资源直接由Nginx处理(提升性能)location ~ .*\.(js|css|png|jpg|gif)$ {root /data/static; # 静态资源目录expires 30d; # 客户端缓存30天}
}
2.3 重启Nginx并检查配置
# 测试配置文件语法
nginx -t# 重启服务
systemctl restart nginx# 查看状态(确保active状态)
systemctl status nginx
步骤3:验证负载均衡效果
3.1 测试请求分发
# 连续访问5次,观察响应变化
for i in {1..5}; do curl http://192.168.1.10; done
预期输出(根据权重3:2比例交替出现):
Tomcat Server 1
Tomcat Server 1
Tomcat Server 1
Tomcat Server 2
Tomcat Server 2
3.2 模拟节点故障
# 关闭Tomcat节点1
/opt/tomcat1/bin/shutdown.sh# 再次测试(应全部由节点2响应)
curl http://192.168.1.10 # 返回 "Tomcat Server 2"
3.3 查看Nginx节点状态
# 安装状态模块
apt install nginx-module-status -y # 添加配置到nginx.conf
location /nginx_status {stub_status;allow 127.0.0.1; # 只允许本机访问deny all;
}# 重启后访问
curl http://127.0.0.1/nginx_status
关键指标:
Active connections: 3
server accepts handled requests
15 15 45 # 总连接数/成功握手/总请求
Reading: 0 Writing: 1 Waiting: 2
步骤4:Session共享解决方案(附加)
4.1 安装Redis
# 在独立服务器安装Redis
wget https://download.redis.io/releases/redis-7.0.12.tar.gz
tar xzf redis-7.0.12.tar.gz
cd redis-7.0.12
make && make install
4.2 配置Tomcat Session管理器 4.2 配置 Tomcat Session 管理器
修改所有Tomcat的 context.xml
:
<Context><Manager className="org.apache.catalina.session.PersistentManager"><Store className="org.apache.catalina.session.RedisStore"host="192.168.1.13" # Redis服务器IPport="6379"database="0"password="your_redis_pass"/></Manager>
</Context>
四、常见问题与解决方案
-
Tomcat节点宕机,请求仍被分配?
解决:确保配置了
max_fails
和fail_timeout
,Nginx会自动屏蔽故障节点。 -
Session丢失(如登录状态)
方案1:使用
ip_hash
定向同一用户到固定Tomcat(不适用于动态IP)。
方案2(推荐):整合Redis实现Session共享。 -
Nginx报502 Bad Gateway Nginx 报 502 Bad Gateway
-
检查Tomcat是否启动:
curl http://192.168.1.11:8080
-
查看防火墙:
firewall-cmd --list-ports
(开放8080/9090) -
调整Nginx超时时间:
-
proxy_connect_timeout 60s;
proxy_read_timeout 600s;
-
静态资源加载失败
-
确保
location ~ .*\.(js|css|png)$
路径正确,文件存在于/data/static/
目录。
-
五、总结与心得
-
核心价值:
-
吞吐量提升3倍+(实测3台Tomcat集群QPS可达单机3倍)。
-
实现业务零停机更新(逐台重启Tomcat)。
-
-
优化方向:
-
引入Redis集中管理Session。
-
使用Nginx缓存高频请求数据。
-
监控工具(如Prometheus)实时分析节点负载。
-
-
踩坑提醒:
权重分配需根据服务器性能动态调整,避免低配服务器过载!
上线前务必用JMeter进行压力测试,验证集群稳定性。