nginx + lua + kafka实现日志监控
nginx + lua + kafka实现日志监控
前言
1)架构图:

2)方案:
- 1:线上请求打向nginx后,使用lua完成日志整理:如统一日志格式,过滤无效请求,分组等。
- 2:根据不同业务的nginx日志,划分不同的topic。
- 3:lua实现producter异步发送到kafka集群。
- 4:对不同日志感兴趣的业务组实时消费获取日志数据。
3)技术框架
- openresty: http://openresty.org
- kafka: http://kafka.apache.org
- lua-resty-kafka: https://github.com/doujiang24/lua-resty-kafka
当前目录: /home/nginx
一、安装openresty
1)下载openresty
wget https://openresty.org/download/openresty-1.11.2.5.tar.gz
2)解压openresty
tar -zxvf openresty-1.11.2.5.tar.gz
3)重命名
mv openresty-1.11.2.5 openresty
4)安装openresty
# --prefix:代表安装的路径 --with:需要加入的构建的包 --without:需要排除的包
./configure --prefix=/home/nginx/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module
make
make install
如遇报错详见第六章!!!!
二、安装lua-resty-kafka
wget https://github.com/doujiang24/lua-resty-kafka/archive/master.zip
unzip lua-resty-kafka-master.zip -d /home/nginx/
三、部署kafka
四、nginx配置
1)进入openresty
-
cd /home/nginx/openresty
-
vim nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
# 将所有kafka的lua脚本加载到nginx中
lua_package_path "/home/nginx/lua-resty-kafka/lua-resty-kafka-master/lib/?.lua;;";
#gzip on;
server {
listen 8081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/nginx/nginx-1.22.0/html/web_html;
index index.html index.htm;
}
location /api {
proxy_pass http://127.0.0.1:18081/api; # 转发规则
proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 8;
proxy_send_timeout 8;
proxy_read_timeout 8;
proxy_buffer_size 4k;
proxy_buffers 512 8k;
proxy_busy_buffers_size 8k;
proxy_temp_file_write_size 64k;
proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header;
root html;
index index.html index.htm;
proxy_http_version 1.1;
# 使用log_by_lua 包含lua代码,因为log_by_lua指令运行在请求最后且不影响proxy_pass机制
log_by_lua '
-- 引入lua所有api
local cjson = require "cjson"
local producer = require "resty.kafka.producer"
-- 定义kafka broker地址,ip需要和kafka的host.name配置一致
local broker_list = {
{ host = "192.168.1.31", port = 9092 },
}
-- 定义json便于日志数据整理收集
local log_json = {}
log_json["uri"]=ngx.var.uri
log_json["args"]=ngx.var.args
log_json["host"]=ngx.var.host
log_json["request_body"]=ngx.var.request_body
log_json["remote_addr"] = ngx.var.remote_addr
log_json["remote_user"] = ngx.var.remote_user
log_json["time_local"] = ngx.var.time_local
log_json["status"] = ngx.var.status
log_json["body_bytes_sent"] = ngx.var.body_bytes_sent
log_json["http_referer"] = ngx.var.http_referer
log_json["http_user_agent"] = ngx.var.http_user_agent
log_json["http_x_forwarded_for"] = ngx.var.http_x_forwarded_for
log_json["upstream_response_time"] = ngx.var.upstream_response_time
log_json["request_time"] = ngx.var.request_time
-- 转换json为字符串
local message = cjson.encode(log_json);
-- 定义kafka异步生产者
local bp = producer:new(broker_list, { producer_type = "async" })
-- 发送日志消息,send第二个参数key,用于kafka路由控制:
-- key为nill(空)时,一段时间向同一partition写入数据
-- 指定key,按照key的hash写入到对应的partition
local ok, err = bp:send("test", nil, message)
if not ok then
ngx.log(ngx.ERR, "kafka send err:", err)
return
end
';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
五、检测&运行
# 检测配置,只检测nginx配置是否正确,lua错误日志在nginx的error.log文件中
./nginx -t /home/openresty/nginx/conf/nginx.conf
# 启动
./nginx -c /home/openresty/nginx/conf/nginx.conf
# 重启
./nginx -s reload
六、编译失败
!!!!nginx版本不兼容的情况

发生原因: nginx版本不兼容!!!
在openresty make时,会出现 src/os/unix/ngx_user.c:36:7: 错误:‘struct crypt_data’没有名为‘current_salt’的成员
解决方式:
1)编辑nix_user.c
vim /home/nginx/openresty/build/nginx-1.11.2/src/os/unix/ngx_user.c
2)注释 cd.current_salt[0] = ~salt[0];

3)重新编译
make && make install 即可

浙公网安备 33010602011771号