1.Tomcat核心组件
flowchart LR
Client --> Connector
Connector --> Engine
Engine --> Host
Host --> Context
Context --> Servlet
| 组件 |
作用 |
| Server |
Tomcat 实例,负责启动/关闭 |
| Service |
管理 Connector 和 Engine |
| Connector |
接收 HTTP/AJP 请求 |
| Engine |
请求处理引擎 |
| Host |
虚拟主机(类似 Nginx server) |
| Context |
Web 应用(一个项目) |
| Servlet |
业务处理 |
2.反向代理架构
flowchart LR
Client --> Nginx
Nginx --> Tomcat1
Nginx --> Tomcat2
Tomcat1 --> MySQL
Tomcat2 --> MySQL
作用:
- 负载均衡
- HTTPS 卸载
- 隐藏 Tomcat
- 静态资源处理
- 高可用
3.Nginx反向代理配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| upstream tomcat { server 192.168.1.101:8080; server 192.168.1.102:8080; }
server { listen 80; location / { proxy_pass http://tomcat; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
4.Tomcat关键配置
1 2 3 4 5
| <Connector port="8080" protocol="HTTP/1.1" maxThreads="200" URIEncoding="UTF-8" connectionTimeout="20000"/>
|
关键参数:
| 参数 |
说明 |
| port |
监听端口 |
| maxThreads |
最大线程数 |
| connectionTimeout |
连接超时 |
| URIEncoding |
URL 编码 |
5.生产推荐架构
1 2 3 4 5 6 7 8 9 10 11 12 13
| Client │ HTTPS │ Nginx │ HTTP │ Tomcat 集群 │ Redis(Session) │ MySQL
|
推荐实践:
- Nginx 处理 HTTPS
- Tomcat 仅处理业务
- Session 存 Redis(或采用无状态 Token/JWT)
- 多台 Tomcat 通过 Nginx 负载均衡
- 静态资源交由 Nginx 提供