使用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工具定期压缩归档日志,避免占用磁盘空间。参考来源: