首页 > 系统相关 >NGINX统计网站的PV、UV、独立IP

NGINX统计网站的PV、UV、独立IP

时间:2022-11-24 19:14:17浏览次数:62  
标签:PV 1.1 IP UV cookie User Cookie message Nov

NGINX: 统计网站的PV、UV、独立IP

Nginx: PV、UV、独立IP

做网站的都知道,平常经常要查询下网站PV、UV等网站的访问数据,当然如果网站做了CDN的话,nginx本地的日志就没什么意义了,下面就对nginx网站的日志访问数据做下统计;

概念:

  • UV(Unique Visitor):独立访客,将每个独立上网电脑(以cookie为依据)视为一位访客,一天之内(00:00-24:00),访问您网站的访客数量。一天之内相同cookie的访问只被计算1次
  • PV(Page View):访问量,即页面浏览量或者点击量,用户每次对网站的访问均被记录1次。用户对同一页面的多次访问,访问量值累计
  • 统计独立IP:00:00-24:00内相同IP地址只被计算一次,做网站优化的朋友最关心这个

 

先声明下环境,此次运行的nginx版本1.7,后端Tomcat运行的是动态交互程序(需进行用户认证,如果是静态页面则抓不到cache值,$http_cookie是空值),就是这样;

nginx日志文件配置

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - [$time_local]  "$request" '
                        ' - $status "User_Cookie:$guid" ';
 #User_Cookie为日志显示字符,$guid为变量,具体内容在下面定义,也可在日志格式里写入$http_cookie 显示完整的cookie内容<br>
    sendfile        on;
    keepalive_timeout  65;
        upstream backserver {
        ip_hash;
        server 1.1.2.2:8080;
        server  1.1.2.3:8080;
}
server {
        listen       80;
        server_name  localhost;
       #if ( $http_cookie ~* "(.*)$")  匹配所有内容
       if ( $http_cookie ~* "CSID=([A-Z0-9]*)"){
                set $guid $1;
        }    #只匹配CSID字符信息,此处为正则表达式<br>
        access_log  logs/host.access.log  main;
         location ~* ^(.*)$ {
             #limit_req zone=allips burst=1 nodelay;
 
             proxy_pass  http://backserver;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header REMOTE-HOST $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             client_max_body_size 8m;
             }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

注:$http_cookie这个里面的值是一个一个cookie的值,中间以“;”分隔

日志输出格式

192.168.40.2 - [02/Nov/2016:15:44:35 +0800]  "GET /wcm/app/main/refresh.jsp?r=1478072325778 HTTP/1.1"  - 200 "User_Cookie:7F00000122A5597C46607B1C0A7EC016" 
192.168.40.2 - [02/Nov/2016:15:44:35 +0800]  "GET /webpic/W0201611/W020161102/W020161102566715167404.jpg HTTP/1.1"  - 200 "User_Cookie:7F00000122A5597C46607B1C0A7EC016" 
119.255.31.109 - [02/Nov/2016:15:44:36 +0800]  "GET /wcm/app/main/refresh.jsp?r=1478072510132 HTTP/1.1"  - 200 "User_Cookie:7F000001237921BE9237838AEC65704D" 
119.255.31.109 - [02/Nov/2016:15:44:36 +0800]  "GET /wcm/app/message/message_query_service.jsp?READFLAG=0&MSGTYPES=1%2C2%2C3 HTTP/1.1"  - 200 "User_Cookie:7F000001237921BE9237838AEC65704D" 
192.168.40.2 - [02/Nov/2016:15:44:37 +0800]  "GET /wcm/app/message/message_query_service.jsp?READFLAG=0&MSGTYPES=1%2C2%2C3 HTTP/1.1"  - 200 "User_Cookie:7F00000123D3BF2345115EAAC21F71E0" 
192.168.40.2 - [02/Nov/2016:15:44:37 +0800]  "GET /wcm/app/message/message_query_service.jsp?READFLAG=0&MSGTYPES=1%2C2%2C3 HTTP/1.1"  - 200 "User_Cookie:7F00000123EF73896DF98EDA9950944E" 
192.168.40.2 - [02/Nov/2016:15:44:37 +0800]  "GET /wcm/app/message/message_query_service.jsp?READFLAG=0&MSGTYPES=1%2C2%2C3 HTTP/1.1"  - 200 "User_Cookie:7F00000123FE0F9C397E1A8F0C4F044B" 
192.168.40.2 - [02/Nov/2016:15:44:37 +0800]  "GET /wcm/app/main/refresh.jsp?r=1478072511427 HTTP/1.1"  - 200 "User_Cookie:7F00000123A465B7EA1DE0AF0AE671B7" 
119.255.31.109 - [02/Nov/2016:15:44:38 +0800]  "GET /wcm/app/message/message_query_service.jsp?READFLAG=0&MSGTYPES=1%2C2%2C3 HTTP/1.1"  - 200 "User_Cookie:7F00000123D89B11302DF80AE773C900" 

 

PV统计

可统计单个链接地址访问量:

[root@localhost logs]# grep index.shtml host.access.log | wc -l

总PV量:

[root@localhost logs]# awk '{print $6}' host.access.log | wc -l

独立IP 

[root@localhost logs]# awk '{print $1}' host.access.log | sort -r |uniq -c | wc -l

UV统计

[root@localhost logs]# awk '{print $10}' host.access.log | sort -r |uniq -c |wc -l

 

Cookie 测试页面

关于种cookie,可以使用下面的html代码,编辑,添加需要种的cookie

#index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<meta http-equiv="Refresh" content="10"> //为了方便测试,每10秒刷新一次页面
</head>
<body>
<h1>test.test.com域测试</h1>
下面列出了该域的cookie<br>
<p>
<script>
document.cookie="guid=A1UD8E5512451111111111"; //种cookie,追加
document.cookie="city=beijing"; //种cookie,追加
document.write(document.cookie); //列出已经存在的
</script>
</p>
</body>
</html>

标签:PV,1.1,IP,UV,cookie,User,Cookie,message,Nov
From: https://www.cnblogs.com/Lqdream/p/16922879.html

相关文章

  • 网络性能评估工具Iperf详解
     一、网络性能评估工具Iperf网络性能评估主要是监测网络带宽的使用率,将网络带宽利用最大化是保证网络性能的基础,但是由于网络设计不合理、网络存在安全漏洞等原因,都会导......
  • UVA1327 King's Quest
    King'sQuest题意对于每个王子,寻找他喜欢并且可以娶的女孩,统计,按顺序输出。思路可以考虑建图。在输入时,如果王子喜欢女孩,那么建一条从王子到女孩的边;如果女孩可以......
  • 小程序 XQTypeScriptFramework 使用
    说明XQTypeScriptFramework隶属于XQFramework下JS基础性框架部分XQFramework励志将开发将常用开发语言基础性框架统一汇总,为全站开发使用到的基础语法进行统一,拜......
  • JavaScript的this指向
    1、结论:js中的this是当前方法所属的对象 'usestrict'letobj={name:'taotao',myName(){returnthis}}console.log(obj.myName())//{nam......
  • NOIP2022 游记
    NOIP2022游记Hello,hello,helloworld.Iopenmyeyesandsaidhellototheworld.——《HelloWorld》Day-2被hh的神秘模拟赛打自闭了。2hard4me.希望......
  • error inflating class com.baidu.mapapi.map.mapview
    在使用百度地图的SDK时,加载com.baidu.mapapi.map.MapView这个界面的时候报错,错误是errorinflatingclasscom.baidu.mapapi.map.mapview解决方法我仔细观察了百度提供的示......
  • Chapter 2: Python Language Basics, IPython, and Jupyter Notebooks 个人理解与问题
    2.2IPython基础2.2.2运行Jupyternotebook  在终端输入\(Jupyter\quadnotebook\),会在默认浏览器打开\(Jupyter\),但是注意路径问题,如果我们在D:\Python_Code\IPy......
  • IPv6改造方案:隧道技术-中科三方
    前面我们介绍过了IPv6改造的第一种技术方案:双栈技术,本文我们将简单介绍下IPv6改造的第二种技术方案——隧道技术。隧道技术(Tunneling)隧道技术是为了实现IPv6点到点的访问......
  • s3 upload files with shell script
    TL;DR#!/usr/bin/envbash#hangj.cnblogs.coms3_key="Q3AM3UQ867SPQQA43P2F"s3_secret="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"session_token="Security-Tok......
  • 【Cocos2d-X(2.x) 游戏开发系列之三】最新版本cocos2d­2.0­x­2.0.2使用新资源加载
    本站文章均为​​ 李华明Himi ​​​原创,转载务必在明显处注明:​​​​​前段时间cocos2dx更新了最新版本cocos2d­2.0­x­2.0.2,也从这个版本开始对于资源加载与管理都......