Count PV, UV and IP of website through nginx

Keywords: Web Server Nginx PHP

Recently, there is a need for the project to count PV, UV and IP of the website and display them to the background. So we can use the log of nginx.

concept

UV: independent visitors; based on cookie s, if a computer is equipped with three different browsers, opening the same page respectively, three UVs will be generated.
PV: the number of visits; each time a page is visited or refreshed, a PV will be generated.
IP: independent IP address. Based on the public IP, each independent IP accessing this page will generate an IP address.

nginx configuration

Configure the following information in the conf file of vhost, please set the permission of this conf file to 777, otherwise the log file cannot be generated automatically.

#Log format
log_format tongji '$remote_addr - [$time_iso8601]  "$request" '
                        ' - $status "User_Cookie:$guid" ';

server {
    listen      80;
    server_name xxx.com;
    index index.html index.htm index.php;
    root /alidata/www/tongji;
    #Save the part with key as guid, value as letter and number as guid
    if ( $http_cookie ~* "guid=([a-zA-Z0-9]*)"){
        set $guid $1;
    }
    if ($time_iso8601 ~ "(\d{4}-\d{2}-\d{2})") {
        set $date $1;
    }
    #The access log refers to the format of "tongji" and is divided and saved according to the date.
    access_log /alidata/www/nginx_log/access_$date.log tongji;
    location ~* ^(.*)$ {
        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;
    }
}

js file

The following js file is introduced into the page where uv statistics are needed to generate a cookie expired on the same day.

var cookie = {
    //The remaining milliseconds of the day
    leftTime: function() {
        var curTamp = new Date().getTime();
        //The time stamp in the early morning of the day, minus one millisecond, is to prevent the subsequent time from reaching the state of 00:00:00
        var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
        var passedTamp = curTamp - curWeeHours;
        var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
        return leftTamp;
    },
    //n: key name, v: key value, exp: expiration time (ms)
    setCookie: function(n, v, exp) {
        var date = new Date()
        date.setTime(date.getTime() + exp);
        document.cookie = n + "=" + escape(v) +
            ((exp == null) ? "" : ";expires=" + date.toGMTString())
    },
    //n is the key name of the key value you want to get
    getCookie: function(n) {
        var reg = /\s/g;
        var result = document.cookie.replace(reg, "");
        var resultArr = result.split(";");
        for (var i = 0; i < resultArr.length; i++) {
            var nameArr = resultArr[i].split("=");
            if (nameArr[0] == n) {
                return nameArr[1];
            }
        }
    }
};

//Generate random id
var guid = function() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
};
//Generate a guid if it does not exist
console.log(cookie.leftTime() / 1000 / 60);
!cookie.getCookie('guid') && cookie.setCookie('guid', guid(), cookie.leftTime());
document.write(document.cookie);

log file

The contents are as follows

61.141.xxx.xxx - [2019-05-16T15:18:34+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 
61.141.xxx.xxx - [2019-05-16T15:18:35+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 
61.141.xxx.xxx - [2019-05-16T15:18:35+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 
61.141.xxx.xxx - [2019-05-16T15:18:35+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 
61.141.xxx.xxx - [2019-05-16T15:18:35+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 
61.141.xxx.xxx - [2019-05-16T15:18:35+08:00]  "GET /ttt.html HTTP/1.1"  - 304 "User_Cookie:032284f362a63e3d375f8176aad4e0d7" 

Log analysis

command

/ / statistics IP
 awk '{print }' xxx/access.log (your log file path) | sort -r |uniq -c | wc -l
 / / statistics PV
 awk '{print }' xxx/access.log (your log file path) | wc -l
 / / statistics UV
 awk '{print }' xxx/access.log (your log file path) | sort -r |uniq -c |wc -l

Posted by w.geoghegan on Sat, 09 Nov 2019 08:53:52 -0800