RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
NGINX怎么统计网站的PV、UV、独立IP

本篇内容介绍了“NGINX怎么统计网站的PV、UV、独立IP”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

专注于为中小企业提供网站设计制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业乐安免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了近1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

概念:

  • 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内容
  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字符信息,此处为正则表达式
    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
 



 //为了方便测试,每10秒刷新一次页面


test.test.com域测试

下面列出了该域的cookie

“NGINX怎么统计网站的PV、UV、独立IP”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


当前名称:NGINX怎么统计网站的PV、UV、独立IP
本文URL:http://sczitong.cn/article/ghjhpe.html