Nginx 访问统计 增加html静态网页显示 每晚定时更新
优采云 发布时间: 2020-08-09 16:44想统计自己网站的访问量,之前试过百度统计,在每位网页降低一些文件,百度会自己帮忙搜集统计。其实假如你自己网站就是用Nginx挂在服务器上的,他自己才会帮你统计,你只须要把他整理下就可以。我简单整理了下,可以看先疗效:
如果你希望这样简单的统计下自己小网站的访问量可以象我这样简单操作下:
1. 修改Nginx 导出日志
whereis nginx
cd /etc/nginx/
ls -l
sudo vim nginx.conf
修改nginx access_log 到指定目录
2. 写脚本 对日志进行统计并写入html,并清空日志
#!/bin/bash
cd /home/ubuntu/Blog
date1=`date +%F`
date2=`date +%r`
ips=`awk '{print $1"
"}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 10`
pv=`awk '{print $7}' access.log | wc -l`
ip=`awk '{print $1}' access.log | sort -n | uniq -c | wc -l`
html=`awk '{print $7"
"}' access.log|sort | uniq -c |grep -v '.jpg'|grep -v '.ico'|grep -v '.php'|grep -v '.do'|grep -v '400'|grep -v '.svg'|grep -v 'cgi-bin'|grep -v '.css'|grep -v '.js'|grep -v '.png'|grep -v '.jsp'|grep -v '.gif' |sort -n -k 1 -r | head -n 20`
echo "
--------${date1}--${date2}-------------
Pv is $pv
Ip is $ip
$ips ...
$html ...
----------
" >> count.html | sort -rn count.html | sed -i '31d' count.html | sort -r
:> access.log
:> error.log
3.脚本每晚晚上定时执行
crontab -e # 添加每天凌晨12:01 执行脚本
01 00 * * * /home/ubuntu/Blog/statistics.sh
4.如果有须要的话网页只能特定ip访问