# /etc/nginx/nginx.conf 主配置优化
worker_processes auto; # 自动匹配CPU核心数
worker_rlimit_nofile 100000; # 提高worker进程文件描述符限制
events {
worker_connections 4000; # 每个worker进程的最大连接数
use epoll; # Linux高效事件模型
multi_accept on; # 同时接受多个连接
}
http {
sendfile on; # 启用高效文件传输
tcp_nopush on; # 优化数据包发送
tcp_nodelay on; # 禁用Nagle算法
keepalive_timeout 30; # 保持连接超时
keepalive_requests 1000; # 单个连接最大请求数
types_hash_max_size 2048;
server_tokens off; # 隐藏NGINX版本信息
# 启用Gzip压缩
gzip on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_proxied any;
# 静态文件缓存
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# 其他配置...
}
server {
listen 80;
server_name example.com;
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
access_log off;
}
# 反向代理到PM2管理的Node.js应用
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;
}
}
# 全局安装PM2
npm install pm2@latest -g
# 启动应用
pm2 start app.js --name "my-app"
# 常用命令
pm2 list # 查看运行中的应用
pm2 monit # 监控应用
pm2 logs # 查看日志
pm2 reload all # 重载所有应用
pm2 delete my-app # 删除应用
module.exports = {
apps: [{
name: 'my-app',
script: 'app.js',
instances: 'max', // 根据CPU核心数自动扩展
exec_mode: 'cluster', // 集群模式
autorestart: true,
watch: false,
max_memory_restart: '1G', // 内存超过1G自动重启
env: {
NODE_ENV: 'production',
PORT: 3000
},
env_production: {
NODE_ENV: 'production',
PORT: 3000
}
}]
};
# 安装PM2监控模块
pm2 install pm2-server-monit
# 设置自动扩展规则 (基于CPU或内存)
pm2 scale my-app +2 # 增加2个实例
pm2 scale my-app 4 # 设置固定4个实例
# /etc/sysctl.conf 添加以下内容
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
fs.file-max = 100000
# 应用修改
sysctl -p
# 使用systemd限制PM2资源 (创建/etc/systemd/system/pm2.service)
[Unit]
Description=PM2 process manager
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure
ExecStart=/usr/local/bin/pm2 resurrect
ExecReload=/usr/local/bin/pm2 reload all
ExecStop=/usr/local/bin/pm2 kill
[Install]
WantedBy=multi-user.target
# 重新加载并启用
systemctl daemon-reload
systemctl enable pm2
# 安装htop用于实时监控
apt install htop -y
# 安装glances综合监控工具
pip install glances
# 为PM2日志设置logrotate (/etc/logrotate.d/pm2)
/root/.pm2/pm2.log /root/.pm2/logs/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
copytruncate
}
#!/bin/bash
# 清理旧日志
find /var/log/nginx -type f -name "*.log" -mtime +7 -delete
find /root/.pm2/logs -type f -name "*.log" -mtime +7 -delete
# 重启服务保持新鲜
pm2 reload all
systemctl reload nginx
# 检查更新
apt update && apt upgrade -y
npm update -g pm2
通过以上配置,您的VPS服务器将能够更高效地管理系统资源,处理更多并发请求,同时保持系统的稳定性。记得根据实际负载情况调整参数值,并定期监控系统性能。