亲宝软件园·资讯

展开

详解NGINX怎样统计网站的PV、UV、独立IP

人气:0

Nginx: PV、UV、独立IP

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

概念:

先声明下环境,此次运行的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> 
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论