使用Nginx日志監控可從配置、分析、工具及告警等方面入手,具體如下:
/var/log/nginx/access.log,錯誤日志:/var/log/nginx/error.log。$request_time記錄響應時間):在nginx.conf中使用log_format指令。命令行基礎分析
awk '{print $9}' access.log | sort | uniq -c | sort -nr。awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -n 10。tail -f error.log | grep "404\|500"。工具化分析
goaccess access.log -o report.html。Nginx狀態模塊
stub_status模塊,通過http://服務器IP/nginx_status查看實時連接數、請求量等。nginx.conf中添加location /nginx_status { stub_status on; }。異常告警
logrotate工具定期壓縮歸檔日志,避免占用磁盤空間。參考來源: