Web日志分析是运维、安全和开发人员的重要工作,可以帮助了解网站流量、识别异常行为、优化性能等。以下是Linux环境下Web日志分析的方法和技巧。
Apache:
/var/log/apache2/access.log
(访问日志)/var/log/apache2/error.log
(错误日志)Nginx:
/var/log/nginx/access.log
(访问日志)/var/log/nginx/error.log
(错误日志)# 查看实时日志
tail -f /var/log/nginx/access.log
# 查看最后100行
tail -n 100 /var/log/nginx/access.log
# 查看特定时间段的日志
sed -n '/10\/Oct\/2023:10:00/,/10\/Oct\/2023:11:00/p' /var/log/nginx/access.log
# 统计总请求数
wc -l /var/log/nginx/access.log
# 统计独立IP数量
awk '{print $1}' /var/log/nginx/access.log | sort | uniq | wc -l
# 统计访问量最高的10个IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
# 统计HTTP状态码分布
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
# 统计最频繁的URL请求
awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20
# 统计特定IP的访问情况
awk '$1 == "192.168.1.100" {print $0}' /var/log/nginx/access.log
# 查找包含特定字符串的日志
grep "POST /login" /var/log/nginx/access.log
# 查找404错误
grep ' 404 ' /var/log/nginx/access.log
# 查找特定时间段的日志
grep '10/Oct/2023:1[0-2]:' /var/log/nginx/access.log
# 安装
sudo apt-get install goaccess # Debian/Ubuntu
sudo yum install goaccess # CentOS/RHEL
# 使用
goaccess /var/log/nginx/access.log -a
# 安装
sudo apt-get install awstats # Debian/Ubuntu
sudo yum install awstats # CentOS/RHEL
# 配置后生成报告
/usr/lib/cgi-bin/awstats.pl -config=yourdomain -update
# 安装
sudo apt-get install logwatch # Debian/Ubuntu
sudo yum install logwatch # CentOS/RHEL
# 运行
logwatch --detail High
# 查找可能的SQL注入尝试
grep -i -E "union.*select|sleep\(|benchmark\(|concat\(|information_schema" /var/log/nginx/access.log
# 查找可能的XSS尝试
grep -i -E "<script|alert\(|onerror=|onload=" /var/log/nginx/access.log
# 查找目录遍历尝试
grep -i -E "\.\./|\.\.\\" /var/log/nginx/access.log
# 统计登录页面的失败尝试
grep "POST /login" /var/log/nginx/access.log | awk '$9 == 401 || $9 == 403 {print $1}' | sort | uniq -c | sort -nr
# 示例logrotate配置 (/etc/logrotate.d/nginx)
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
/etc/init.d/nginx reload > /dev/null
endscript
}
# 在Nginx配置中设置按日期记录日志
access_log /var/log/nginx/access-$year-$month-$day.log;
# 安装ELK Stack后配置Logstash处理Web日志
input {
file {
path => "/var/log/nginx/access.log"
start_position => "beginning"
}
}
filter {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
date {
match => [ "timestamp", "dd/MMM/yyyy:HH:mm:ss Z" ]
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
}
}
# 使用Promtail收集日志并发送到Loki
scrape_configs:
- job_name: nginx
static_configs:
- targets:
- localhost
labels:
job: nginx
__path__: /var/log/nginx/*.log
#!/bin/bash
# 监控异常状态码
ALERT_FILE="/tmp/web_alert.log"
LOG_FILE="/var/log/nginx/access.log"
THRESHOLD=10
# 检查过去5分钟内5xx错误
count=$(awk -v d1="$(date --date="-5 min" "+%d/%b/%Y:%H:%M")" -v d2="$(date "+%d/%b/%Y:%H:%M")" \
'$0 > d1 && $0 < d2 && $9 >= 500' $LOG_FILE | wc -l)
if [ $count -gt $THRESHOLD ]; then
echo "$(date) - High 5xx errors detected: $count" >> $ALERT_FILE
# 可以添加邮件或短信通知
fi
通过以上方法和工具,您可以有效地分析Linux环境下的Web日志,及时发现并解决问题,优化网站性能,并增强安全性。