在Debian上優化Node.js應用的性能可以通過多個方面來實現,以下是一些關鍵的優化策略:
sudo apt update && sudo apt upgrade -y
ulimit -n 65535
為了使這個設置在重啟后仍然有效,編輯 /etc/security/limits.conf
文件,添加以下內容:* soft nofile 65535
* hard nofile 65535
/etc/sysctl.conf
文件,添加或修改以下參數:net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.ip_local_port_range = 1024 65535
然后運行 sudo sysctl -p
使更改生效。curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install 16
nvm use 16
fs.promises.readFile()
代替同步的 fs.readFileSync()
。npm install pm2 -g
pm2 start app.js --name my-app
通過上述方法,可以顯著提高Node.js應用在Debian系統上的性能和穩定性。記得在優化后進行性能測試,以確保優化措施有效。