以下是在Ubuntu上排查Nginx性能問題的常用方法:
top
、htop
查看CPU、內存占用,iostat
、iotop
分析磁盤I/O,nload
、iftop
監控網絡流量。ps aux | grep nginx
確認進程狀態,systemctl status nginx
查看服務是否正常。netstat -tulnp | grep nginx
或ss -tulnp | grep nginx
確認監聽端口。/var/log/nginx/error.log
,統計高頻錯誤(如連接拒絕、超時)。/var/log/nginx/access.log
,使用awk
篩選慢請求(如響應時間>5秒),或通過goaccess
生成可視化報告。slowlog_path
和slowlog_timeout
,記錄超時請求。ab
(Apache Bench)或wrk
模擬高并發請求,觀察響應時間、吞吐量等指標。# ab測試示例(-n請求總數,-c并發數)
ab -n 1000 -c 100 http://your-server.com/
nginx.conf
中的worker_processes
(建議與CPU核心數一致)、worker_connections
等參數,優化連接數限制。stub_status
模塊查看基礎狀態(需在配置中添加location /stub_status
),或使用nginx-module-vts
獲取詳細流量數據。proxy_pass
、proxy_connect_timeout
等參數是否合理。curl
手動訪問后端接口,檢查響應時間,排查數據庫或緩存異常。通過以上步驟,可快速定位Nginx性能瓶頸并針對性優化。