在 Debian 上提升 Node.js 性能,可以从系统层、Node 运行时、代码架构、部署方式几个层面来做。下面按实用程度整理一份清单。
Debian 自带版本往往偏旧:
node -v
建议用 Node 18 / 20 / 22 LTS(官方或 NodeSource):
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
Node 高并发时容易遇到:
EMFILE: too many open files
修改:
# /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
并确认:
ulimit -n
sysctl -w net.core.somaxconn=65535
sysctl -w net.ipv4.tcp_tw_reuse=1
--max-old-space-size防止内存不足崩溃:
node --max-old-space-size=2048 app.js
NODE_ENV=productionexport NODE_ENV=production
很多库(Express 等)会关闭调试和视图缓存。
node --optimize-for-size --jitless=false app.js
一般不建议乱加,除非 profiling 后明确需要。
Node 默认是单线程,必须手动利用多核。
const cluster = require('cluster');
const os = require('os');
if (cluster.isMaster) {
os.cpus().forEach(() => cluster.fork());
} else {
require('./app');
}
npm i -g pm2
pm2 start app.js -i max
pm2 startup
pm2 save
PM2 提供:
❌ 错误:
const data = fs.readFileSync('big.json');
✅ 正确:
fs.promises.readFile('big.json');
fs.createReadStream('big.log')
.pipe(...)
gzip on;
gzip_types text/plain application/json;
Nginx + Node 配合效果明显。
生产环境:
console.lognode --prof app.js
node --prof-process isolate-*.log
pm2 monit
或:
✅ Debian + Node 20 LTS
✅ PM2(cluster 模式)
✅ Nginx 反向代理
✅ Redis 缓存
✅ 结构化日志
如果你愿意,可以告诉我:
我可以给你更针对性的优化方案。