在 CentOS 系统中,Apache 服务器的访问日志通常位于 /var/log/httpd/ 目录下,主要的访问日志文件是 access_log
grep -o 'http.*"GET' /var/log/httpd/access_log | awk '{print $7}' | cut -d'"' -f1 | sort | uniq -c | sort -nr
这个命令的解释如下:
grep -o 'http.*"GET' /var/log/httpd/access_log:从访问日志中提取所有 GET 请求。awk '{print $7}':提取请求的 URL。cut -d'"' -f1:去除 URL 中的双引号。sort:对 URL 进行排序。uniq -c:统计每个 URL 的访问次数。sort -nr:按照访问次数降序排序。这个命令会输出每个 URL 及其对应的访问次数。如果你想查看特定时间范围内的访问量,可以使用 awk 命令来过滤日志。例如,要查看 2021 年 10 月的访问量,可以使用以下命令:
awk '$4 >= "[10/Oct/2021" && $4 <= "[31/Oct/2021"' /var/log/httpd/access_log | grep -o 'http.*"GET' | awk '{print $7}' | cut -d'"' -f1 | sort | uniq -c | sort -nr
这个命令会在 access_log 文件中查找 2021 年 10 月的日志条目,然后按照上述步骤统计访问量。