点击流日志采集服务部署
1、服务器中安装依赖
yum -y install gcc perl pcre-devel openssl openssl-devel
2、上传LuaJIT-2.0.4.tar.gz并安装LuaJIT
tar -zxvf LuaJIT-2.0.4.tar.gz -C /usr/local/src/
cd /usr/local/src/LuaJIT-2.0.4/
make && make install PREFIX=/usr/local/luajit
3、设置LuaJIT环境变量
vi /etc/profile 添加如下内容:
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
source /etc/profile
4、创建modules保存nginx的模块
mkdir -p /usr/local/nginx/modules
5、上传依赖的模块
Ø set-misc-nginx-module-0.29.tar.gz
Ø lua-nginx-module-0.10.0.tar.gz
Ø ngx_devel_kit-0.2.19.tar.gz
Ø echo-nginx-module-0.58.tar.gz
6、将依赖的模块直接解压到/usr/local/nginx/modules目录
tar -zxvf lua-nginx-module-0.10.0.tar.gz -C /usr/local/nginx/modules/
tar -zxvf set-misc-nginx-module-0.29.tar.gz -C /usr/local/nginx/modules/
tar -zxvf ngx_devel_kit-0.2.19.tar.gz -C /usr/local/nginx/modules/
tar -zxvf echo-nginx-module-0.58.tar.gz -C /usr/local/nginx/modules/
不需要编译安装
7、安装openresty
下载对应的安装包:openresty-1.9.7.3.tar.gz
解压:tar -zxvf openresty-1.9.7.3.tar.gz -C /usr/local/src/
编译安装openresty:
cd /usr/local/src/openresty-1.9.7.3/
执行命令:
./configure --prefix=/usr/local/openresty --with-luajit && make && make install
8、安装nginx
下载对应的安装包:nginx-1.8.1.tar.gz
解压:tar -zxvf nginx-1.8.1.tar.gz -C /usr/local/src/
9、编译nginx并支持其他模块
进入到nginx的安装目录 cd /usr/local/src/nginx-1.8.1/
执行如下命令:
./configure --prefix=/usr/local/nginx \
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" \
--add-module=/usr/local/nginx/modules/ngx_devel_kit-0.2.19 \
--add-module=/usr/local/nginx/modules/lua-nginx-module-0.10.0 \
--add-module=/usr/local/nginx/modules/set-misc-nginx-module-0.29 \
--add-module=/usr/local/nginx/modules/echo-nginx-module-0.58
make -j2
make install
10、修改nginx的配置文件
worker_processes 2;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
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";
access_log logs/access.log tick;
sendfile on;
keepalive_timeout 65;
server { listen 80; server_name localhost; 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 /var/nginx_logs/ma.log tick; #输出空字符串 echo ''; } } } |
11、修改在nginx所在的服务器上添加一个ma.js
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://xxx.xxx.xxx.xxx/log.gif?' + args; })();
|
12、在要统计的页面添加js脚本
<script type="text/javascript"> var _maq = _maq || []; _maq.push(['_setAccount', 'zx5352']);
(function() { var ma = document.createElement('script'); ma.type = 'text/javascript'; ma.async = true; ma.src = 'http://xxx.xxx.xxx.xxx/ma.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ma, s); })(); </script> |
13、启动nginx
cd /usr/local/nginx 目录
执行命令 sbin/nginx -c conf/nginx.conf 启动nginc服务
检验启动是否成功:
(1)ps -ef | grep nginx 查看是否有nginx对应的进程
(2) netstat -nlp 查看80端口是否起来
通过游览器访问对应的web界面 http//xxx.xxx.xxx.xxx:80
并且观察日志采集文件是否有对应的内容输出
tail -f /var/nginx_logs/ma.log
tail -f logs/access.log