以下是在Ubuntu上进行Nginx性能测试的常用方法,涵盖工具使用和关键步骤:
ApacheBench(ab)
sudo apt update && sudo apt install apache2-utils
wrk
sudo apt install wrk
JMeter
ab -n 1000 -c 100 http://localhost/
-n 1000:总请求数-c 100:并发连接数wrk -t4 -c200 -d30s http://localhost/
-t4:4个线程-c200:200个并发连接-d30s:持续30秒动态接口测试
-H添加请求头,-p指定POST数据文件。ab -n 500 -c 50 -H "Content-Type: application/json" -p data.json http://localhost/api
长连接测试
ab -n 1000 -c 50 -k http://localhost/
-k:启用HTTP KeepAlive,测试长连接下的性能表现。实时监控Nginx状态
stub_status):location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
访问http://localhost/nginx_status查看活跃连接数、请求速率等指标。系统资源监控
top、htop查看CPU/内存占用,iftop监控网络带宽,ss -ant查看TCP连接状态。调优建议
worker_processes auto;
worker_connections 10000;
use epoll;
keepalive_timeout 65;
并通过ulimit -n提升文件描述符限制。参考资料: