1. 程式人生 > 其它 >詳解NGINX如何統計網站的PV、UV、獨立IP

詳解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日誌檔案配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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統計

可統計單個連結地址訪問量:

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

總PV量:

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

獨立IP

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

UV統計

1 [root@localhost logs]# awk '{print $10}' host.access.log | sort -r |uniq -c |wc -l
學習時的痛苦是暫時的 未學到的痛苦是終生的