Nginx: PV、UV、獨立IP
做網(wǎng)站的都知道,平常經(jīng)常要查詢下網(wǎng)站PV、UV等網(wǎng)站的訪問數(shù)據(jù),當然如果網(wǎng)站做了CDN的話,nginx本地的日志就沒什么意義了,下面就對nginx網(wǎng)站的日志訪問數(shù)據(jù)做下統(tǒng)計;
概念:
- UV(Unique Visitor):獨立訪客,將每個獨立上網(wǎng)電腦(以cookie為依據(jù))視為一位訪客,一天之內(nèi)(00:00-24:00),訪問您網(wǎng)站的訪客數(shù)量。一天之內(nèi)相同cookie的訪問只被計算1次
- PV(Page View):訪問量,即頁面瀏覽量或者點擊量,用戶每次對網(wǎng)站的訪問均被記錄1次。用戶對同一頁面的多次訪問,訪問量值累計
- 統(tǒng)計獨立IP:00:00-24:00內(nèi)相同IP地址只被計算一次,做網(wǎng)站優(yōu)化的朋友最關(guān)心這個
先聲明下環(huán)境,此次運行的nginx版本1.7,后端Tomcat運行的是動態(tài)交互程序(需進行用戶認證,如果是靜態(tài)頁面則抓不到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為變量,具體內(nèi)容在下面定義,也可在日志格式里寫入$http_cookie 顯示完整的cookie內(nèi)容<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 ~* "(.*)$") 匹配所有內(nèi)容
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統(tǒng)計
可統(tǒng)計單個鏈接地址訪問量:
[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統(tǒng)計
[root@localhost logs]# awk '{print $10}' host.access.log | sort -r |uniq -c |wc -l
Cookie 測試頁面
關(guān)于種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); //列出已經(jīng)存在的
</script>
</p>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。