


How does NGINX count the PV, UV, and independent IP of the website?
May 19, 2023 am 09:13 AM概念:
uv(unique visitor):獨(dú)立訪客,將每個(gè)獨(dú)立上網(wǎng)電腦(以cookie為依據(jù))視為一位訪客,一天之內(nèi)(00:00-24:00),訪問(wèn)您網(wǎng)站的訪客數(shù)量。一天之內(nèi)相同cookie的訪問(wèn)只被計(jì)算1次
PV(頁(yè)面瀏覽量)是指用戶每次訪問(wèn)網(wǎng)站時(shí)所產(chǎn)生的點(diǎn)擊量或頁(yè)面瀏覽量,記錄為1次。用戶對(duì)同一頁(yè)面的多次訪問(wèn),訪問(wèn)量值累計(jì)
統(tǒng)計(jì)獨(dú)立ip:00:00-24:00內(nèi)相同ip地址只被計(jì)算一次,做網(wǎng)站優(yōu)化的朋友最關(guān)心這個(gè)
先聲明下環(huán)境,此次運(yùn)行的nginx版本1.7,后端tomcat運(yùn)行的是動(dòng)態(tài)交互程序(需進(jìn)行用戶認(rèn)證,如果是靜態(tài)頁(yè)面則抓不到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)容在下面定義,也可在日志格式里寫(xiě)入$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字符信息,此處為正則表達(dá)式<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這個(gè)里面的值是一個(gè)一個(gè)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)計(jì)
可統(tǒng)計(jì)單個(gè)鏈接地址訪問(wèn)量:
[root@localhost logs]# grep index.shtml host.access.log | wc -l
總pv量:
[root@localhost logs]# awk '{print $6}' host.access.log | wc -l
獨(dú)立ip
[root@localhost logs]# awk '{print $1}' host.access.log | sort -r |uniq -c | wc -l
uv統(tǒng)計(jì)
[root@localhost logs]# awk '{print $10}' host.access.log | sort -r |uniq -c |wc -l
cookie 測(cè)試頁(yè)面
關(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"> //為了方便測(cè)試,每10秒刷新一次頁(yè)面 </head> <body> <h1>test.test.com域測(cè)試</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>
The above is the detailed content of How does NGINX count the PV, UV, and independent IP of the website?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur
