Openresty collecting front-end logs

Source: Internet
Author: User

Environment: centos-6.5

Dependency: Yum install-y gcc gcc-c++

Download 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

Unzip all



CD openresty-1.9.15.1

Compile and install (specify Pcre and OpenSSL's extract directory):

./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


Custom log format:

A. Front-end unified scripting Ma.js

(function  ()  {    var params = {};    // Document Object Data     if (document)  {         params.domain = document.domain | |   ';          params.url = document. url | |   ';          params.title = document.title | |   ';          params.referrer = document.referrer  | |   ";      }       //window Object Data      if (Window && window.screen)  {         params.sh = window.screen.height | |  0;        params.sw = window.screen.width | |  0;        params.cd = window.screen.colordepth | |  0;    }       //navigator Object Data      if (navigator)  {        params.lang =  navigator.language | |   ";      }       //parsing _maq configuration      if (_maq)  {        for (Var i in _maq)  {             switch (_maq[i][0])  {                 case  ' _setaccount ':                      params.account = _maq[i][1];                     break;                 default:                     break;            }            }       }        //stitching parameter String     var args =  ';      for (Var i in params)  {         if (args !=  ')  {             args +=  ' & ';        }            args += i +  ' = '  + encodeuricomponent (params[ I]);     }         //request back-end script via image object     var img =  new image (1, 1);     img.src =  ' Http://analytics.codinglabs.org/1.gif? '  + args;}) ();


B. Buried point code (embedded in the front page)

<script type= "Text/javascript" >var _maq = _maq | | [];_maq.push ([' _setaccount ', ' website identifier ']);(function () {var ma = document.createelement (' script '); ma.type = ' Text/javascri PT ';    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 intercept 1.gif requests, customize the log format, configure 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, recorded by Subrequest log             access_log off;                      access_by_lua  "                 --  User Tracking cookie is named __ Utrace                local  uid = ngx.var.cookie___utrace                         if not uid  then                     --  generates a trace cookie if it is not, the algorithm is MD5 (timestamp +ip+ client information)              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;UID&NBSP;=&NBSP;NGX.MD5 (Ngx.now ()   &NBSP;NGX.VAR.REMOTE_ADDR&NBSP, ....  ngx.var.http_user_agent)                  end                  ngx.header[' Set-cookie '] = {' __utrace= '   &NBSP;UID&NBSP, ....   ';  path=/'}                 if ngx.var.arg_domain then                 --  record logs via subrequest to/i-log, and bring the parameters and user tracking cookie over                       Ngx.location.capture ('/i-log? ') &NBSP, .... &NBSP;NGX.VAR.ARGS&NBSP, ....   ' &utrace= '   &NBSP;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 X1 's Empty GIF image              empty_gif;        }                   location /i-log {              #内部location, external direct access is not allowed              internal;                       #设置变量, pay attention to 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, preferably in the actual application buffer, in the format tick             access_log /home/qun/nginx/openresty/nginx/logs/ma.log tick;                       #输出空字符串              echo  ';         }           }

Attention:

1.log_format tick custom log format 2.location/1.gif intercept all 1.gif requests and GET request parameters 3.location/i-log log landed


Log:

cat/home/qun/nginx/openresty/nginx/logs/ma.log1466110654.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


Openresty collecting front-end logs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.