插件窝 干货文章 简明教程: 如何使用NGINX和PM2优化VPS服务器的资源管理

简明教程: 如何使用NGINX和PM2优化VPS服务器的资源管理

NGINX PM2 proxy 配置 329    来源:    2025-04-11

NGINX与PM2优化VPS服务器资源管理指南

1. NGINX基础配置优化

1.1 安装NGINX

sudo apt update
sudo apt install nginx -y

1.2 基础性能优化配置

编辑/etc/nginx/nginx.conf

user www-data;
worker_processes auto; # 自动设置工作进程数为CPU核心数
pid /run/nginx.pid;

events {
    worker_connections 1024; # 每个工作进程的最大连接数
    multi_accept on; # 同时接受多个连接
    use epoll; # 使用高效的epoll事件模型
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    # 关闭不必要的服务器标记
    server_tokens off;

    # 启用Gzip压缩
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # 日志设置
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

2. PM2安装与配置

2.1 安装Node.js和PM2

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
sudo npm install pm2@latest -g

2.2 基本PM2命令

# 启动应用
pm2 start app.js --name "my-app"

# 列出所有应用
pm2 list

# 监控应用
pm2 monit

# 查看日志
pm2 logs

# 保存当前进程列表
pm2 save

# 设置开机启动
pm2 startup

2.3 高级PM2配置

创建ecosystem.config.js文件:

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"
    }
  }]
}

3. NGINX与PM2集成

3.1 反向代理配置

编辑NGINX站点配置文件:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000; # 假设PM2运行在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;
    }

    # 静态文件处理
    location /static/ {
        alias /path/to/static/files/;
        expires 30d;
        access_log off;
    }
}

3.2 负载均衡配置(多PM2实例)

upstream node_app {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    keepalive 64;
}

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://node_app;
        # 其他proxy设置同上
    }
}

4. 高级优化技巧

4.1 资源限制

# 限制PM2进程内存使用
pm2 start app.js --max-memory-restart 500M

# 限制CPU使用
pm2 start app.js --max-restarts 10

4.2 监控与日志管理

# 安装PM2监控模块
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M # 单个日志文件最大10MB
pm2 set pm2-logrotate:retain 30 # 保留30个日志文件

# 安装监控仪表板
pm2 install pm2-server-monit

4.3 安全加固

# 限制请求大小
client_max_body_size 10m;

# 限制请求速率
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

location /api/ {
    limit_req zone=one burst=20;
    # 其他配置
}

5. 性能测试与调优

5.1 压力测试

# 安装Apache Benchmark
sudo apt install apache2-utils

# 执行测试
ab -n 1000 -c 100 http://yourdomain.com/

5.2 根据测试结果调优

  • 调整NGINX的worker_connections
  • 调整PM2的instances数量
  • 优化数据库连接池大小
  • 添加适当的缓存策略

通过以上配置和优化,您的VPS服务器应该能够更高效地管理资源,处理更多并发请求,同时保持稳定性。