ARTICLE

DMIT 性能调优:内核、Nginx、数据库与应用层

从系统内核到 Nginx、数据库和应用层的性能调优方法,含瓶颈定位与检查清单。

性能调优的思路

DMIT 云服务器性能调优不是一次性工作,而是一个「测量 → 分析 → 调整 → 验证」的循环。盲目修改配置往往适得其反。本文提供从系统层到应用层的调优方法。

所有调优建议基于通用 Linux 最佳实践,不涉及对 DMIT 底层基础设施的修改。

第一步:建立基线

调优前先记录当前性能指标作为基线,否则无法判断调优效果:

# 系统负载基线
uptime
cat /proc/loadavg

# CPU 信息
nproc
lscpu | grep "Model name"

# 内存基线
free -h
cat /proc/meminfo | head -5

# 磁盘 I/O 基线
dd if=/dev/zero of=/tmp/test bs=1M count=500 oflag=direct 2>&1 | tail -1
rm /tmp/test

# 网络基线
ping -c 10 8.8.8.8
curl -o /dev/null -s -w "Connect: %{time_connect}s\nTTFB: %{time_starttfb}s\nTotal: %{time_total}s\n" https://www.dmit.io

将基线数据保存到文件中,调优后对比。

系统层调优

内核参数

调整 /etc/sysctl.conf 优化网络和内存:

# TCP 连接优化
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_keepalive_time = 300

# 内存优化
vm.swappiness = 10
vm.overcommit_memory = 1

# 文件描述符
fs.file-max = 65535

应用后执行 sysctl -p。

文件描述符限制

高并发服务需要更多文件描述符。编辑 /etc/security/limits.conf:

* soft nofile 65535
* hard nofile 65535

对于 systemd 服务,编辑 /etc/systemd/system/your-service.service:

[Service]
LimitNOFILE=65535

Nginx 调优

Worker 配置

worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 16384;
    use epoll;
    multi_accept on;
}

HTTP 配置

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    
    # Gzip 压缩
    gzip on;
    gzip_min_length 1024;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/javascript;
    
    # 缓存
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    
    # 超时
    keepalive_timeout 30;
    keepalive_requests 100;
    client_body_timeout 10;
    client_header_timeout 10;
}

FastCGI 缓存(PHP-FPM)

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=php:100m inactive=10m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache php;
fastcgi_cache_valid 200 301 302 10m;
fastcgi_cache_use_stale error timeout invalid_header;

数据库调优

MySQL/MariaDB

# /etc/mysql/mariadb.conf.d/50-server.cnf
[mysqld]
innodb_buffer_pool_size = 1G   # 约为 RAM 的 50-70%
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_type = 0            # MySQL 8.0 已移除,设 0 兼容
max_connections = 200
thread_cache_size = 16
table_open_cache = 512

PostgreSQL

# /etc/postgresql/15/main/postgresql.conf
shared_buffers = 1GB            # 约为 RAM 的 25%
effective_cache_size = 3GB       # 约为 RAM 的 75%
work_mem = 16MB
maintenance_work_mem = 256MB
max_connections = 100
random_page_cost = 1.1           # SSD 优化
effective_io_concurrency = 200   # SSD 优化

应用层调优

Node.js

# 使用 cluster 模式利用多核
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    for (let i = 0; i < numCPUs; i++) cluster.fork();
} else {
    // your app
}

# PM2 进程管理
pm2 start app.js -i max
pm2 startup
pm2 save

Python (Gunicorn)

gunicorn app:app \
    --workers $(nproc) \
    --worker-class uvicorn.workers.UvicornWorker \
    --bind 0.0.0.0:8000 \
    --timeout 30 \
    --max-requests 1000 \
    --max-requests-jitter 50

资源监控与瓶颈定位

CPU 瓶颈

# 查看 CPU 使用最高的进程
top -b -n1 | head -20
ps aux --sort=-%cpu | head -10

# 查看系统调用
strace -c -p <pid> -e trace=all 2>&1 | sort -n

内存瓶颈

# 查看内存使用
ps aux --sort=-%mem | head -10

# 查看 OOM 记录
dmesg | grep -i "out of memory"

# 检查 swap 使用
swapon --show

磁盘 I/O 瓶颈

# 查看 I/O 等待
iostat -x 1 5
iotop -o    # 仅显示有 I/O 的进程

# 查看 I/O 等待的 CPU 占比
top -b -n1 | grep "wa"

网络瓶颈

# 查看连接状态分布
ss -s

# 查看 TIME_WAIT 数量
ss -ant | grep TIME-WAIT | wc -l

# 带宽测试(需安装 speedtest-cli)
speedtest-cli

调优检查清单

层面项目目标
系统内核参数TCP 连接队列充足
系统文件描述符≥ 65535
系统swapswappiness=10
Nginxworkerauto + 16384 连接
Nginxgzip开启,level 4
Nginx缓存open_file_cache 已配置
数据库缓冲池RAM 的 50-70%
数据库连接数max_connections 合理
应用多核利用cluster/PM2/workers
监控基线已记录调优前后可对比

更多相关文章:《监控与备份》、SSH 安全加固、《初次部署检查清单》。

本文核对日期:2026-08-18。调优参数以你的实际负载和资源为准。

NEXT STEP

看完教程,选一台合适的 DMIT

用套餐快照按预算与线路筛选,或直接前往官网查看实时库存。

推广披露:通过链接购买可能为推荐者带来佣金,不会增加你的购买价格。