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

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

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

#!/bin/bash
# nginx日志文件路径  
LOG_FILE="/usr/local/nginx/logs/access.log"  
#统计访问IP
#_index=$(grep "GET /public/index.html" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)    
#_pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F' ' '{print $1}' | sort | uniq -c)   
#统计访问链接 
_index=$(grep "GET /public/index.html" "$LOG_FILE" |awk -F'[][]' '{print $2}')    
_pdf=$(grep "GET /public/11.15.pdf" "$LOG_FILE" | awk -F'[][]' '{print $2}')  
#结果写入日志     
echo "$_index" > public_index_access.log
echo "$_pdf" > public_pdf_access.log
#进行统计
echo "index.html access:<hr>" > public_access.log
cut -d: -f1 public_index_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "<br>"}' >> public_access.log  
echo "<br><br>pdf access:<hr>" >> public_access.log
cut -d: -f1 public_pdf_access.log | sort | uniq -c| awk '{print $2, $1}'| awk '{print $0, "<br>"}'  >> public_access.log
sleep 1 
# 将访问统计数据插入到HTML页面中
ACCESS_STATS="$(cat public_access.log)"
html_content='
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>访问统计</title>
</head>
<body>
    <h1>访问统计数据</h1>
    <div id="access_stats">'$ACCESS_STATS'</div>
</body>
</html>
'

# 将内容写入 HTML 文件
echo "$html_content" > index.html

# 输出成功信息
echo "HTML file generated: index.html"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值