在 Debian 上优化 JSP(Java Server Pages)性能,通常是从 系统层面 + Java 运行环境 + Web 容器 + JSP/代码层面 多管齐下。下面按层次给你一套实用方案(以 Debian + Tomcat 最常见场景为例)。
JSP 应用通常并发连接多:
# 查看
ulimit -n
# 修改(永久)
sudo vim /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
systemctl disable bluetooth
systemctl disable cups
ext4 或 xfsdefaults,noatime
java -version
编辑:
/etc/default/tomcat9
# 或
$TOMCAT_HOME/bin/setenv.sh
示例(4G 内存):
JAVA_OPTS="-server \
-Xms2g -Xmx2g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:+HeapDumpOnOutOfMemoryError"
✅ 建议:
conf/server.xml
<Connector
port="8080"
protocol="org.apache.coyote.http11.Http11Nio2Protocol"
maxThreads="500"
minSpareThreads="50"
acceptCount="200"
connectionTimeout="20000"
enableLookups="false"
compression="on"
compressableMimeType="text/html,text/xml,text/css,application/javascript"
/>
✅ 重点:
JSP 本质是 Servlet,编译慢会影响性能。
<servlet>
<servlet-name>jsp</servlet-name>
<init-param>
<param-name>development</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>genStringAsCharArray</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
✅ 生产环境:
development=false(禁止热编译)trimSpaces=true(减少输出)在部署前用 jspc 编译 JSP:
tomcat9-jasper
或使用 Maven 插件:
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>jspc-maven-plugin</artifactId>
</plugin>
✅ 效果:
❌ 不推荐:
<%
// 大量业务逻辑
%>
✅ 推荐:
<%@ page cache="true" %>
<%@ page session="false" %>
Nginx 示例:
location ~* \.(jpg|css|js)$ {
expires 30d;
}
jstat -gcjvisualvmarthasmanager 应用✅ Debian + Tomcat 生产环境:
development=false如果你愿意,可以告诉我:
我可以给你精准的配置文件。