在 Debian 上“保护 Node.js”通常指几个不同层面,下面按最常见场景系统说明,你可以按需取用。
永远不要以 root 运行 Node 服务:
adduser nodeapp
su - nodeapp
启动方式:
node app.js
或用 systemd 指定用户:
[Service]
User=nodeapp
Group=nodeapp
编辑服务文件 /etc/systemd/system/nodeapp.service:
[Service]
ExecStart=/usr/bin/node /opt/app/app.js
User=nodeapp
WorkingDirectory=/opt/app
# 安全限制
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
ReadOnlyPaths=/
ReadWritePaths=/opt/app/logs
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_BIND_SERVICE
systemctl daemon-reload
systemctl enable --now nodeapp
使用 nginx 反向代理:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
}
}
Node 只监听 127.0.0.1:
app.listen(3000, '127.0.0.1');
ufw allow 22
ufw allow 80
ufw allow 443
ufw enable
使用 NodeSource 或 nvm:
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
检查版本:
node -v
npm -v
定期审计:
npm audit
npm audit fix
生产环境安装:
npm ci --omit=dev
evalnpm install helmet
const helmet = require('helmet');
app.use(helmet());
chown -R nodeapp:nodeapp /opt/app
chmod -R 750 /opt/app
ReadWritePaths=/opt/app/logs
ReadOnlyPaths=/opt/app
journalctl -u nodeapp -f
安装 fail2ban 防暴力:
apt install fail2ban
apt install unattended-upgrades
dpkg-reconfigure unattended-upgrades
❌ 杀毒软件保护 Node
❌ 只靠防火墙
✅ 最小权限 + 反向代理 + 依赖审计
如果你能说明:
我可以给你更精确的 Debian + Node.js 安全方案。