NGINX怎麼統計網站的PV、UV、獨立IP
概念:
uv(unique visitor):独立访客,将每个独立上网电脑(以cookie为依据)视为一位访客,一天之内(00:00-24:00),访问您网站的访客数量。一天之内相同cookie的访问只被计算1次
PV(页面浏览量)是指用户每次访问网站时所产生的点击量或页面浏览量,记录为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>
以上是NGINX怎麼統計網站的PV、UV、獨立IP的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

解決 Nginx 跨域問題有兩種方法:修改跨域響應頭:添加指令以允許跨域請求,指定允許的方法和頭,以及設置緩存時間。使用 CORS 模塊:啟用模塊並配置 CORS 規則,允許跨域請求、方法、頭和設置緩存時間。

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。
