• 根据nginx日志统计页面访问次数


     静态页面部署在nginx上,页面只有查看下载功能。

    需求是统计每条访问次数和下载次数,根据日志分析写了一个shell脚本,触发脚本后生成一个html可以远程查看统计的数量。

    1. #!/bin/bash
    2. # nginx日志文件路径
    3. LOG_FILE="/usr/local/nginx/logs/access.log"
    4. #统计访问IP
    5. #_index=$(grep "GET /public/index.html" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)
    6. #_pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)
    7. #统计访问链接
    8. _index=$(grep "GET /public/index.html" "$LOG_FILE" |awk -F'[][]' '{print $2}')
    9. _pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F'[][]' '{print $2}')
    10. #结果写入日志
    11. echo "$_index" > public_index_access.log
    12. echo "$_pdf" > public_pdf_access.log
    13. #进行统计
    14. echo "index.html access:
      "
      > public_access.log
    15. cut -d: -f1 public_index_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "
      "}'
      >> public_access.log
    16. echo "

      pdf access:
      "
      >> public_access.log
    17. cut -d: -f1 public_pdf_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "
      "}'
      >> public_access.log
    18. sleep 1
    19. # 将访问统计数据插入到HTML页面中
    20. ACCESS_STATS="$(cat public_access.log)"
    21. html_content='
    22. 访问统计
    23. 访问统计数据

    24. '$ACCESS_STATS'
    25. '
    26. # 将内容写入 HTML 文件
    27. echo "$html_content" > index.html
    28. # 输出成功信息
    29. echo "HTML file generated: index.html"

  • 相关阅读:
    【JAVA】LinkedList与链表(Part1)
    Python进阶教学——装饰器与闭包
    errgroup的常见误用
    Day30-Docker学习教程系列(二)-Docker架构
    聊聊前端性能指标那些事儿
    LeetCode 593. 有效的正方形(向量做法)
    c++静态链接库的简单创建与使用
    kodi的IPTV直播源爬取
    冰冰学习笔记:new与delete
    【计算机视觉】图像聚类&噪声
  • 原文地址:https://blog.csdn.net/qq_36802726/article/details/134516133