详解NGINX如何统计网站的PV、UV、独立IP
Nginx:PV、UV、独立IP
做网站的都知道,平常经常要查询下网站PV、UV等网站的访问数据,当然如果网站做了CDN的话,nginx本地的日志就没什么意义了,下面就对nginx网站的日志访问数据做下统计;
概念:
- UV(UniqueVisitor):独立访客,将每个独立上网电脑(以cookie为依据)视为一位访客,一天之内(00:00-24:00),访问您网站的访客数量。一天之内相同cookie的访问只被计算1次
- PV(PageView):访问量,即页面浏览量或者点击量,用户每次对网站的访问均被记录1次。用户对同一页面的多次访问,访问量值累计
- 统计独立IP:00:00-24:00内相同IP地址只被计算一次,做网站优化的朋友最关心这个
先声明下环境,此次运行的nginx版本1.7,后端Tomcat运行的是动态交互程序(需进行用户认证,如果是静态页面则抓不到cache值,$http_cookie是空值),就是这样;
nginx日志文件配置
http{ includemime.types; default_typeapplication/octet-stream; log_formatmain'$remote_addr-[$time_local]"$request"' '-$status"User_Cookie:$guid"'; #User_Cookie为日志显示字符,$guid为变量,具体内容在下面定义,也可在日志格式里写入$http_cookie显示完整的cookie内容
sendfileon; keepalive_timeout65; upstreambackserver{ ip_hash; server1.1.2.2:8080; server1.1.2.3:8080; } server{ listen80; server_namelocalhost; #if($http_cookie~*"(.*)$")匹配所有内容 if($http_cookie~*"CSID=([A-Z0-9]*)"){ set$guid$1; }#只匹配CSID字符信息,此处为正则表达式
access_loglogs/host.access.logmain; location~*^(.*)${ #limit_reqzone=allipsburst=1nodelay; proxy_passhttp://backserver; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerREMOTE-HOST$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; client_max_body_size8m; } error_page500502503504/50x.html; location=/50x.html{ roothtml; } }
注:$http_cookie这个里面的值是一个一个cookie的值,中间以“;”分隔
日志输出格式
192.168.40.2-[02/Nov/2016:15:44:35+0800] "GET/wcm/app/main/refresh.jsp?r=1478072325778HTTP/1.1" -200"User_Cookie:7F00000122A5597C46607B1C0A7EC016"
192.168.40.2-[02/Nov/2016:15:44:35+0800] "GET/webpic/W0201611/W020161102/W020161102566715167404.jpgHTTP/1.1" -200"User_Cookie:7F00000122A5597C46607B1C0A7EC016"
119.255.31.109-[02/Nov/2016:15:44:36+0800] "GET/wcm/app/main/refresh.jsp?r=1478072510132HTTP/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%2C3HTTP/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%2C3HTTP/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%2C3HTTP/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%2C3HTTP/1.1" -200"User_Cookie:7F00000123FE0F9C397E1A8F0C4F044B"
192.168.40.2-[02/Nov/2016:15:44:37+0800] "GET/wcm/app/main/refresh.jsp?r=1478072511427HTTP/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%2C3HTTP/1.1" -200"User_Cookie:7F00000123D89B11302DF80AE773C900"
PV统计
可统计单个链接地址访问量:
[root@localhostlogs]#grepindex.shtmlhost.access.log|wc-l
总PV量:
[root@localhostlogs]#awk'{print$6}'host.access.log|wc-l
独立IP
[root@localhostlogs]#awk'{print$1}'host.access.log|sort-r|uniq-c|wc-l
UV统计
[root@localhostlogs]#awk'{print$10}'host.access.log|sort-r|uniq-c|wc-l
Cookie测试页面
关于种cookie,可以使用下面的html代码,编辑,添加需要种的cookie
#index.html//为了方便测试,每10秒刷新一次页面 test.test.com域测试
下面列出了该域的cookie