详解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>

以ä¸å°±æ¯æ¬æçå¨é¨å容ï¼å¸æ对大家ç学习ææ帮å©ï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã

相关推荐