LuaJIT 安装
tar zxf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make
make install PREFIX=/home/myself/lj2
pcre 安装
tar -zxvf pcre-8.32.tar.gz
cd pcre-8.32
make
make install
Nginx 安装
export LUAJIT_LIB=/path/to/luajit/lib
export LUAJIT_INC=/path/to/luajit/include/luajit-2.1
./configure --prefix=/opt/nginx \
--with-ld-opt="-Wl,-rpath,/path/to/luajit-or-lua/lib" \
--add-module=/path/to/ngx_devel_kit \
--add-module=/path/to/lua-nginx-module
make
make install
user root;
worker_processes 2;
events {
worker_connections 1024;
}
http{
lua_package_path "/home/oicq/guomm/nginx_lua/lua-resty-mysql-master/lib/?.lua;;"; --重要
lua_shared_dict logs 10m;
init_worker_by_lua_block {
local delay = 10
function put_log_into_mysql(premature)
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.log(ngx.ERR,"failed to instantiate mysql: ", err)
return
end
db:set_timeout(1000)
local ok, err, errcode, sqlstate = db:connect{
host = "xxx",
port = 3306,
database = "database_name",
user = "username",
password = "password",
charset = "utf8",
}
if not ok then
ngx.log(ngx.ERR,"failed to connect: ", err, ": ", errcode, " ", sqlstate)
return
end
-- get data from shared dict and put them into mysql
local key = "logs"
local vals = ""
local temp_val = ngx.shared.logs:lpop(key)
while (temp_val ~= nil)
do
vals = vals .. ",".. temp_val
temp_val = ngx.shared.logs:lpop(key)
end
if vals ~= "" then
vals = string.sub(vals, 2,-1)
local command = ("insert into es_visit_record(access_ip,server_ip,access_time,run_time,es_response_time,request_body_byte,run_state,url,post_data) values "..vals)
ngx.log(ngx.ERR,"command is ",command)
local res, err, errcode, sqlstate = db:query(command)
if not res then
ngx.log(ngx.ERR,"insert error: ", err, ": ", errcode, ": ", sqlstate, ".")
return
end
end
local ok, err = db:close()
if not ok then
ngx.log(ngx.ERR,"failed to close: ", err)
return
end
-- decycle call timer to run put_log_into_mysql method, just like crontab
local ok, err = ngx.timer.at(delay, put_log_into_mysql);
if not ok then
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end
end
local ok, err = ngx.timer.at(delay, put_log_into_mysql)
if not ok then
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end
}
upstream elasticsearch_servers {
server xxx max_fails=3 fail_timeout=30s;
server xxx max_fails=3 fail_timeout=30s;
server xx max_fails=3 fail_timeout=30s;
}
log_format porxy '$remote_addr,$upstream_addr,[$time_local],$request,$request_body,$status,$body_bytes_sent,$request_time,$upstream_response_time';
server {
listen 9202;
location / {
proxy_pass http://elasticsearch_servers;
log_by_lua_block{
local currentTime = os.date("%Y-%m-%d %H:%M:%S", os.time())
currentTime = "\"" .. currentTime .. "\""
local req_body = '-'
if ngx.var.request_body then
req_body = ngx.var.request_body
req_body = string.gsub(req_body,"\n","")
--req_body = string.gsub(req_body,"\t","")
end
req_body = "\"" .. req_body .. "\""
local req_status = 0
if ngx.var.status then
req_status = ngx.var.status
end
local req_time = 0
if ngx.var.request_time then
req_time = ngx.var.request_time
end
local req_req = "\"" .. ngx.var.request .. "\""
local remote_addr = "\"" .. ngx.var.http_x_forwarded_for .. "\""
local server_addr = "\"" .. ngx.var.upstream_addr .. "\""
local myparams = ("("..remote_addr..",".. server_addr..","..currentTime..","..ngx.var.request_time .. ",".. ngx.var.upstream_response_time..","..ngx.var.body_bytes_sent..","..ngx.var.status..","..req_req..","..req_body..")")
local key = "logs"
local len,err = ngx.shared.logs:rpush(key, myparams)
if err then
ngx.log(ngx.ERR,"failed to put log vals into shared dict")
return
end
}
}
access_log logs/es_access.log porxy;
}
}
本配置主要解决Nginx向mysql中实时插入日志的问题。
刚开始的时候看了Nginx和mysql的连接模块。比如说nginx-mysql-module,可以连接mysql。但是插入日志时遇到问题,我们知道nginx的执行过程先是location解析并重写阶段,然后是访问权限控制阶段,接着是内容生成阶段,最后是日志记录阶段。mysql访问阶段属于内容生成阶段,所以代理运行的时间和状态,mysql都无法获取的到。因此,这种通过nginx直连mysql的方式无法达到我们的要求。
通过lua脚本在日志生成阶段获取信息,然后将数据插入mysql。nginx有一个限制,无法在log阶段访问socket即无法访问mysql,所以无法在log阶段直接将数据存入mysql。但是可以通过运行包含mysql操作的shell脚本来解决这个问题。但是这个方法有两个弊端:
通过lua + ngx.time.at + lua_mysql + lua.share.dict 解决问题。整个过程如下所示:
不但访问Mysql的Mysql用户需要有操作对应数据库的权限,还需要调用Mysql命令的用户具有访问mysql的权限。授权命令如下:
GRANT ALL PRIVILEGES ON *.* to root@xxx IDENTIFIED BY 'password';
总的来说,Mysql的数据库对应三种编码。Mysql客户端显示数据的编码,连接Mysql用的编码(即数据存入mysql时,数据的编码),Mysql存储用的编码(字段,表,数据库三种格式可能不同)。不管Mysql存储用的编码是什么,只要Mysql客户端显示数据的编码和连接Mysql用的编码相同,数据就能通过mysql客户端正确显示。