环境:centos-6.5
依赖:yum install -y gcc gcc-c++
下载pcre,openssl,openresty
wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
wget https://openresty.org/download/openresty-1.9.15.1.tar.gz
全部解压
cd openresty-1.9.15.1
编译安装(指定pcre和openssl的解压目录):
./configure --prefix=/home/qun/nginx/openresty --with-pcre=/home/qun/nginx/pcre-8.39 --with-http_ssl_module --with-openssl=/home/qun/nginx/openssl-1.0.1t --with-luajit
make
make install
定制日志格式:
a.前端统一脚本ma.js
(function () { var params = {}; //Document对象数据 if(document) { params.domain = document.domain || ‘‘; params.url = document.URL || ‘‘; params.title = document.title || ‘‘; params.referrer = document.referrer || ‘‘; } //Window对象数据 if(window && window.screen) { params.sh = window.screen.height || 0; params.sw = window.screen.width || 0; params.cd = window.screen.colorDepth || 0; } //navigator对象数据 if(navigator) { params.lang = navigator.language || ‘‘; } //解析_maq配置 if(_maq) { for(var i in _maq) { switch(_maq[i][0]) { case ‘_setAccount‘: params.account = _maq[i][1]; break; default: break; } } } //拼接参数串 var args = ‘‘; for(var i in params) { if(args != ‘‘) { args += ‘&‘; } args += i + ‘=‘ + encodeURIComponent(params[i]); } //通过Image对象请求后端脚本 var img = new Image(1, 1); img.src = ‘http://analytics.codinglabs.org/1.gif?‘ + args; })();
b.埋点代码(嵌入到前端页面中)
<script type="text/javascript"> var _maq = _maq || []; _maq.push([‘_setAccount‘, ‘网站标识‘]); (function() { var ma = document.createElement(‘script‘); ma.type = ‘text/javascript‘; ma.async = true; ma.src = (‘https:‘ == document.location.protocol ? ‘https://master:18080/sa‘ : ‘http://master:18080/sa‘) + ‘/js/ma.js‘; alert(ma); var s = document.getElementsByTagName(‘script‘)[0]; s.parentNode.insertBefore(ma, s); })(); </script>
c.nginx拦截1.gif的请求,定制日志格式,配置nginx.conf
log_format tick "$msec^A$remote_addr^A$u_domain^A$u_url^A$u_title^A$u_referrer^A$u_sh^A$u_sw^A$u_cd^A$u_lang^A$http_user_agent^A$u_utrace^A$u_account"; server { listen 18080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /echo { default_type ‘text/plain‘; echo ‘hello echo‘; } location /lua { default_type ‘text/plain‘; content_by_lua ‘ngx.say("hello, lua")‘; } location /1.gif { #伪装成gif文件 default_type image/gif; #本身关闭access_log,通过subrequest记录log access_log off; access_by_lua " -- 用户跟踪cookie名为__utrace local uid = ngx.var.cookie___utrace if not uid then -- 如果没有则生成一个跟踪cookie,算法为md5(时间戳+IP+客户端信息) uid = ngx.md5(ngx.now() .. ngx.var.remote_addr .. ngx.var.http_user_agent) end ngx.header[‘Set-Cookie‘] = {‘__utrace=‘ .. uid .. ‘; path=/‘} if ngx.var.arg_domain then -- 通过subrequest到/i-log记录日志,将参数和用户跟踪cookie带过去 ngx.location.capture(‘/i-log?‘ .. ngx.var.args .. ‘&utrace=‘ .. uid) end "; #此请求不缓存 add_header Expires "Fri, 01 Jan 1980 00:00:00 GMT"; add_header Pragma "no-cache"; add_header Cache-Control "no-cache, max-age=0, must-revalidate"; #返回一个1×1的空gif图片 empty_gif; } location /i-log { #内部location,不允许外部直接访问 internal; #设置变量,注意需要unescape set_unescape_uri $u_domain $arg_domain; set_unescape_uri $u_url $arg_url; set_unescape_uri $u_title $arg_title; set_unescape_uri $u_referrer $arg_referrer; set_unescape_uri $u_sh $arg_sh; set_unescape_uri $u_sw $arg_sw; set_unescape_uri $u_cd $arg_cd; set_unescape_uri $u_lang $arg_lang; set_unescape_uri $u_utrace $arg_utrace; set_unescape_uri $u_account $arg_account; #打开日志 log_subrequest on; #记录日志到ma.log,实际应用中最好加buffer,格式为tick access_log /home/qun/nginx/openresty/nginx/logs/ma.log tick; #输出空字符串 echo ‘‘; } }
注意:
1.log_format tick定制日志格式 2.location /1.gif拦截所有1.gif的请求,并获得请求参数 3.location /i-log 日志落地
日志:
cat /home/qun/nginx/openresty/nginx/logs/ma.log 1466110654.294^A192.168.137.1^Amaster^Ahttp://master:18080/sa/index.html^AHome^Ahttp://master:18080/sa/index.html^A1080^A1920^A24^Azh-CN^AMozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0^Aa005f232c13979e44fc392da9899469d^A\xEF\xBF\xBD\xEF\xBF\xBD\xD5\xBE\xEF\xBF\xBD\xEF\xBF\xBD\xCA\xB6
时间: 2024-10-14 10:48:53