当前位置: 首页 » 综合知识 » it知识 » 正文

NGINX怎么统计网站的PV、UV、独立IP

发布时间:2023-07-29 以下文章来源于网友投稿,内容仅供参考!

概念:

先声明下环境,此次运行的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 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/2022: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/2022:15:44:35 +0800]  "get /webpic/w0202211/w020221102/w020221102566715167404.jpg http/1.1"  - 200 "user_cookie:7f00000122a5597c46607b1c0a7ec016"
119.255.31.109 - [02/nov/2022: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/2022: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/2022: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/2022: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/2022: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/2022: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/2022: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" "www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="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域测试</h1>
下面列出了该域的cookie<br>
<p>
<script>
document.cookie="guid=a1ud8e5512451111111111"; //种cookie,追加
document.cookie="city=beijing"; //种cookie,追加
document.write(document.cookie); //列出已经存在的
</script>
</p>
</body>
</html>
  • • Linux Ecdsa密钥长度选择有何依据

    在Linux

  • • Linux Khook在内核监控中的应用如何

    Linux

  • • Linux Gsoap是否支持异步通信

    GSOAP是

  • • Linux Coremail如何提升用户体验

    提升Linu

  • • Linux Ecdsa算法有哪些局限性

    ECDSA

  • 哎呀音乐钢琴键盘学习《 钢琴主人训练营》 西瓜学琴
    郭蝈 陪练钢琴 30节课时 考级刚需 让孩子每一次练琴都是高质量的
    30天轻松学会五线谱 流行钢琴自学初级教程 牙牙学琴
    流行爵士钢琴实战技巧VIP课 - 继伟 哎呀音乐
    【海上钢琴师】原版 MT1990钢琴谱
    百首经典流行钢琴实战曲集 - 继伟
    雷费尔德电钢琴重锤88键专业考级儿童初学者数码电子钢琴家用
    小练咖 真人钢琴陪练 1v1服务 2999随时退 1课时50分钟 考级刚需
    雅马哈电钢琴88键重锤CLP735智能数码电子钢琴家用专业初学者考级
    【原装进口】Yamaha/雅马哈钢琴 b121 SC2原声静音钢琴
  • 珠海专业调钢琴
  • 天津宝坻区调钢琴
  • 天津静海区钢琴调音
  • 成都简阳市钢琴调律
  • 大连瓦房店市钢琴调音
  • 眉山调钢琴联系方式
  • 惠州大亚湾钢琴调琴师
  • 长治调琴师
  • 厦门湖里区钢琴调音师
  • 上海普陀区钢琴调音师