大数据之nginx+js点击流日志采集服务部署教程
点击流日志采集服务部署
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.logtick;
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.logtick;
#输出空字符串
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 = 'https://xxx.xxx.xxx.xxx/log.gif?' + args;
})();
12、在要统计的页面添加js脚本
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
上一篇: 结果看到狗子实在是忍不住了