# /etc/nginx/nginx.conf
user www-data;
worker_processes auto; # 自动根据CPU核心数设置工作进程
worker_rlimit_nofile 100000; # 增加每个worker进程的文件描述符限制
events {
worker_connections 4000; # 每个worker进程的最大连接数
use epoll; # 使用epoll事件模型(Linux)
multi_accept on; # 同时接受多个新连接
}
http {
sendfile on; # 启用sendfile系统调用
tcp_nopush on; # 仅在sendfile开启时有效,优化数据包发送
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_proxied any;
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;
# 静态文件缓存设置
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# /etc/nginx/sites-available/your-site.conf
server {
listen 80;
listen [::]:80;
server_name yourdomain.com;
# 启用HTTP/2 (需要SSL证书)
listen 443 ssl http2;
listen [::]:443 ssl http2;
# SSL配置
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2|svg)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
access_log off;
}
# 反向代理到Node.js应用
location / {
proxy_pass http://localhost:3000; # 假设Node.js运行在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_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
proxy_buffering on;
proxy_buffer_size 4k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 24k;
proxy_max_temp_file_size 2048m;
}
# 禁用favicon.ico日志
location = /favicon.ico {
access_log off;
log_not_found off;
}
# 禁用robots.txt日志
location = /robots.txt {
access_log off;
log_not_found off;
}
}
# 全局安装PM2
npm install pm2 -g
# 启动应用并设置进程名称
pm2 start app.js --name "my-app"
# 保存当前进程列表
pm2 save
# 生成启动脚本
pm2 startup
# 启用日志轮转
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M # 每个日志文件最大10MB
pm2 set pm2-logrotate:retain 30 # 保留30个日志文件
pm2 set pm2-logrotate:compress true # 压缩旧日志
# 使用集群模式启动应用(根据CPU核心数自动扩展)
pm2 start app.js -i max --name "my-app-cluster"
# 或者手动指定实例数
pm2 start app.js -i 4 --name "my-app-cluster"
# 创建配置文件 ecosystem.config.js
pm2 ecosystem
module.exports = {
apps: [{
name: "my-app",
script: "./app.js",
instances: "max", // 使用所有CPU核心
exec_mode: "cluster", // 集群模式
max_memory_restart: "1G", // 内存超过1GB时重启
env: {
NODE_ENV: "production",
PORT: 3000
},
env_production: {
NODE_ENV: "production",
PORT: 3000
},
// 日志配置
log_date_format: "YYYY-MM-DD HH:mm Z",
error_file: "/var/log/pm2/my-app-error.log",
out_file: "/var/log/pm2/my-app-out.log",
merge_logs: true,
// 高级配置
node_args: "--max-old-space-size=4096", // 设置Node.js内存限制
watch: false, // 生产环境禁用文件监视
ignore_watch: ["node_modules", "logs"],
autorestart: true,
restart_delay: 5000, // 崩溃后5秒重启
min_uptime: "60s", // 运行少于60秒视为异常
listen_timeout: 8000, // 毫秒
kill_timeout: 3000 // 优雅关闭超时
}]
}
# /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 = 8192
net.ipv4.tcp_syncookies = 1
fs.file-max = 100000
# 应用修改
sysctl -p
# /etc/security/limits.conf 添加
* soft nofile 100000
* hard nofile 100000
root soft nofile 100000
root hard nofile 100000
# 重新登录后检查
ulimit -n
# 安装NGINX状态模块
apt install nginx-module-njs
# PM2监控
pm2 monit # 实时监控
pm2 list # 列出所有进程
pm2 show <id> # 查看特定进程详情
pm2 logs # 查看日志
# 安装PM2监控仪表板
pm2 install pm2-server-monit
# 检查NGINX配置
nginx -t
# 重载NGINX配置
systemctl reload nginx
# PM2维护
pm2 update # 更新PM2
pm2 flush # 清空日志
pm2 reset all # 重置所有进程的计数器
# 清理内存缓存(谨慎使用)
sync; echo 3 > /proc/sys/vm/drop_caches
# 安装Apache Benchmark
apt install apache2-utils
# 基本测试
ab -n 1000 -c 100 http://yourdomain.com/
# 更高级的测试工具
# wrk: https://github.com/wg/wrk
# k6: https://k6.io/
# siege: http://www.joedog.org/siege-home/
通过以上配置,您的VPS服务器应该能够处理更高的并发请求,同时保持较低的响应时间。根据您的具体应用和流量模式,可能需要进一步调整这些参数。