分析 Linux 下的 Nginx 日志,通常分为日志位置确认 → 日志格式理解 → 手动/命令行分析 → 工具化分析几个阶段。下面从实用角度系统讲一下。
nginx -t
会显示配置文件路径,例如:
/etc/nginx/nginx.conf
grep -R "access_log" /etc/nginx/
grep -R "error_log" /etc/nginx/
常见默认路径:
/var/log/nginx/access.log/var/log/nginx/error.log$remote_addr - $remote_user [$time_local] "$request"
$status $body_bytes_sent "$http_referer" "$http_user_agent"
示例:
192.168.1.10 - - [12/Apr/2024:10:22:31 +0800] "GET /index.html HTTP/1.1" 200 2326 "https://www.baidu.com" "Mozilla/5.0"
字段含义:
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head
awk '{print $7}' access.log | sort | uniq -c | sort -nr | head
awk '{print $9}' access.log | sort | uniq -c | sort -nr
grep " 404 " access.log
grep " 500 " access.log
grep "12/Apr/2024:10:" access.log | wc -l
grep -i "bot" access.log | awk '{print $1}' | sort | uniq -c
tail -f /var/log/nginx/error.log
connect() failed:后端服务不可用upstream timed out:反向代理超时permission denied:文件权限问题too many open files:连接数限制goaccess access.log -o report.html --log-format=COMBINED
特点:
✅ 网站变慢:
$request_time)✅ 被刷流量:
✅ SEO 问题:
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time';
如果你愿意,可以:
我可以直接帮你写针对性分析命令或脚本。