# 查看整体资源使用(按 CPU 排序)
top
htop
# 检查内存使用
free -h
# 磁盘 I/O 分析
iostat -x 1
iotop
# 网络流量监控
nload # 实时流量
iftop -i eth0 # 按连接排序
# 按 CPU 使用率排序
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
# 按内存使用排序
ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
# 查看工作进程状态
ps aux | grep nginx
# 检查活跃连接数
netstat -ant | grep :80 | wc -l
# 使用 Nginx 状态模块(需提前配置)
curl http://localhost/nginx_status
输出示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
# 检查配置文件中的核心参数
worker_processes auto; # 应与 CPU 核心数匹配
worker_connections 10240; # 单进程最大连接数
worker_rlimit_nofile 100000; # 文件描述符限制
# 实时监控错误日志
tail -f /var/log/nginx/error.log
# 高频错误统计
grep "error" /var/log/nginx/error.log | awk '{print $8}' | sort | uniq -c | sort -nr
# 慢请求分析(需配置 $request_time)
awk '{if ($10 > 1) print $7, $10}' /var/log/nginx/access.log | sort -k2 -nr
location / {
proxy_pass http://backend;
proxy_connect_timeout 5s; # 连接后端超时
proxy_read_timeout 60s; # 读取响应超时
proxy_send_timeout 30s; # 发送请求超时
}
# 手动测试后端响应
curl -v -o /dev/null -s -w "HTTP Code: %{http_code}\nTotal Time: %{time_total}\n" http://backend
# 检查负载均衡状态(需配置 upstream_status)
curl http://localhost/upstream_status
# MySQL 进程列表
mysql -u root -p -e "SHOW FULL PROCESSLIST;"
# Redis 监控
redis-cli info stats | grep instantaneous_ops_per_sec
# 查看 TCP 连接状态
ss -s # 全局统计
ss -tn sport = :80 | awk '{print $1}' | sort | uniq -c # 按状态分类
# 检查 TIME_WAIT 堆积
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
# 检查防火墙规则
iptables -L -n -v
# 测试带宽瓶颈
iperf3 -c -p 5201
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -20
# 动态黑名单(需安装 ngx_http_geoip_module)
geo $block_ip {
default 0;
1.2.3.4 1; # 手动封禁
}
server {
if ($block_ip) {
return 403;
}
}
# 请求速率限制
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=100r/s;
location /api {
limit_req zone=api_limit burst=200;
}
location ~* \.(jpg|css|js)$ {
expires 365d;
add_header Cache-Control "public";
access_log off;
}
gzip on;
gzip_types text/plain text/css application/json application/javascript;
# 增加端口复用范围
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
# 快速回收 TIME_WAIT 连接
sysctl -w net.ipv4.tcp_tw_reuse=1
sysctl -w net.ipv4.tcp_tw_recycle=1
ab
或 wrk
模拟请求,逐步隔离瓶颈点。