1. 程式人生 > >nginx----統計網站訪問量

nginx----統計網站訪問量

1.定義nginx日誌格式 log_format  main  '$remote_addr - [$time_local]  "$request" '               ' - $status "User_Cookie:$http_cookie" ';   2.定義access日誌的輸出位置和呼叫哪個日誌模板 access_log  logs/access.log  main;   日誌內容是這樣的,最後會顯示使用者的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,就直接統計日誌的行數,如果統計UV,就根據cookie去重。

PV統計

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

grep index.shtml host.access.log | wc -l

總PV量:

awk '{print $6}' host.access.log | wc -l

獨立IP:

awk '{print $1}' host.access.log | sort -r |uniq -c | wc -l

UV統計:

awk '{print $10}' host.access.log | sort -r |uniq -c |wc -l