CentOS 上 Apache 日志的查看与分析
一 日志位置与基础查看
tail -f /var/log/httpd/access_log、tail -f /var/log/httpd/error_loggrep "关键字" /var/log/httpd/access_loggrep '192.168.1.1' /var/log/httpd/access_loggrep -E '(\[1[0-9]{3}-10-[0-9]{1,2}\])' /var/log/httpd/access_log二 访问日志分析与常用命令
cat /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c | sort -nr | headgrep "/index.php" /var/log/httpd/access_log | wc -lawk '{print $4}' /var/log/httpd/access_log | cut -d: -f2 | sort | uniq -c | sort -nrawk '$10 ~ /^[0-9]+$/ {print $10, $7}' /var/log/httpd/access_log | sort -k1,1nr | head -20awk '{print $9}' /var/log/httpd/access_log | sort | uniq -c | sort -nr三 错误日志定位与常见问题
tail -f /var/log/httpd/error_loggrep "ERROR" /var/log/httpd/error_logawk '{print $1}' /var/log/httpd/error_log | sort | uniq -c | sort -nrsystemctl status httpdapachectl configtestnetstat -tuln | grep ':80',以及 grep ':443'setenforce 0(仅测试)、firewall-cmd --list-all、firewall-cmd --permanent --add-service=http --add-service=https && firewall-cmd --reloadchown -R apache:apache /path/to/site && chmod -R 755 /path/to/site四 日志轮转与长期分析
/var/log/httpd/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 0640 root adm
sharedscripts
postrotate
/usr/bin/systemctl reload httpd > /dev/null 2>&1 || true
endscript
}
logrotate -d /etc/logrotate.conf -d /etc/logrotate.d/apache2logrotate -f /etc/logrotate.d/apache2五 日志格式与字段说明
%h %l %u %t \"%r\" %>s %b%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"