在 Debian 上优化 Tomcat,通常从 系统层面、JVM 层面、Tomcat 配置层面、应用层面 四个方面入手。下面给你一套实战型优化清单,适用于生产环境(Tomcat 9/10 通用思路)。
编辑 /etc/sysctl.conf:
# 提高网络连接能力
net.core.somaxconn = 4096
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_tw_reuse = 1
# 增加文件描述符
fs.file-max = 1000000
生效:
sysctl -p
编辑 /etc/security/limits.conf:
tomcat soft nofile 65536
tomcat hard nofile 65536
确认:
ulimit -n
编辑(systemd 方式):
systemctl edit tomcat9
写入:
[Service]
Environment="JAVA_OPTS=-server -Xms2g -Xmx2g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/tomcat9/heapdump.hprof -Dfile.encoding=UTF-8"
| 参数 | 作用 |
|---|---|
-Xms = -Xmx |
避免堆抖动 |
| G1GC | 适合 Web 服务 |
| MaxGCPauseMillis | GC 停顿时间 |
| HeapDumpOnOutOfMemoryError | OOM 时自动 dump |
内存建议:
- 小应用:512M–1G
- 中型:2G–4G
- 不要超过物理内存 70%
编辑:
/etc/tomcat9/server.xml
<Connector port="8080"
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
maxThreads="400"
minSpareThreads="50"
maxConnections="8192"
acceptCount="200"
connectionTimeout="20000"
enableLookups="false"
compression="on"
compressionMinSize="2048"
URIEncoding="UTF-8" />
✅ 推荐:
enableLookups=false)<!-- 注释或删除 -->
<!-- <Connector port="8009" protocol="AJP/1.3" /> -->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
✅ 生产环境可选择性关闭
/etc/tomcat9/server.xml
<Host name="localhost" appBase="webapps"
unpackWARs="false"
autoDeploy="false">
✅ Nginx + Tomcat
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
好处:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="localhost_access_log"
suffix=".txt"
pattern="common"
rotatable="true" />
或使用异步日志(logback / log4j2)
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9090
jps
jstat -gc PID
top -H
✅ 小中型服务
你可以直接告诉我:
我可以给你 一份完全定制的 server.xml + JVM 参数配置。