環境:centos-6.5javascript
依賴:yum install -y gcc gcc-c++html
下載pcre,openssl,openresty前端
wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz java
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gznginx
wget https://openresty.org/download/openresty-1.9.15.1.tar.gz
c++
所有解壓算法
cd openresty-1.9.15.1後端
編譯安裝(指定pcre和openssl的解壓目錄):centos
./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 p_w_picpath/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