lua-nginx-module模块实现了通过lua来扩展nginx,而openresty则是把nginx和这些优秀精良的第三方模块打包在了一起,更便于使用了。
官网地址
http://openresty.org/
由于主要用lua来处理,可以看lua的语法等
https://moonbingbing.gitbooks.io/openresty-best-practices/lua/main.html
安装
tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure --with-cc-opt="-I/usr/local/opt/openssl/include/ -I/usr/local/opt/pcre/include/" --with-ld-opt="-L/usr/local/opt/openssl/lib/ -L/usr/local/opt/pcre/lib/"
常见阶段
当一个请求到nginx后,lua一般可以在如下的七个阶段来处理
set_by_lua: 流程分支处理判断变量初始化
rewrite_by_lua: 转发、重定向、缓存等功能(例如特定请求代理到外网)
access_by_lua: IP准入、接口权限等情况集中处理(例如配合iptable完成简单防火墙)
content_by_lua: 内容生成
header_filter_by_lua: 应答HTTP过滤处理(例如添加头部信息)
body_filter_by_lua: 应答BODY过滤处理(例如完成应答内容统一成大写)
log_by_lua: 会话完成后本地异步完成日志记录(日志可以记录在本地,还可以同步到其他机器)
通过lua来自定义nginx日志
nginx的主要配置如下:
log_format api '{"time":"$time_local","ip":"$remote_addr","request":"$request","analysis":"$analysis"}'
set $analysis "hello world";
lua_need_request_body on;
log_by_lua lua/log.lua;
log.lua示例
local access_token = '-'
local user_id = '-'
local user_key = '-'
local username = '-'
local store_id = '-'
local client_os = '-'
local client_os_version = '-'
local client_type = '-'
local client_version = '-'
local request_params = ''
local data_type = ''
local regex = ''
if ngx.var.request_method then
request_params = ngx.var.request_uri
access_token = string.match(request_params, '[?&]access_token=([^&]+)')
user_id = string.match(request_params, '[?&]user_id=([^&]+)')
user_key = string.match(request_params, '[?&]user_key=([^&]+)')
store_id = string.match(request_params, '[?&]store_id=([^&]+)')
client_os = string.match(request_params, '[?&]p10=([^&]+)')
client_os_version = string.match(request_params, '[?&]p7=([^&]+)')
client_type = string.match(request_params, '[?&]p9=([^&]+)')
client_version = string.match(request_params, '[?&]p1=([^&]+)')
if string.upper(ngx.var.request_method) == 'POST' then
if string.sub(ngx.var.request_body, 1, 1) == '{' and string.sub(ngx.var.request_body, -1, -1) == '}' then
data_type = 'json'
request_params = ngx.var.request_body
else
data_type = 'form'
request_params = '&'..ngx.var.request_body
end
if access_token == nil then
regex = data_type == 'form' and '[?&]access_token=([^&]+)' or '"access_token":"(%w+)"'
access_token = string.match(request_params, regex)
end
if user_id == nil then
regex = data_type == 'form' and '[?&]user_id=([^&]+)' or '"user_id":"(%w+)"'
user_id = string.match(request_params, regex)
end
if user_key == nil then
regex = data_type == 'form' and '[?&]user_key=([^&]+)' or '"user_key":"(%w+)"'
user_key = string.match(request_params, regex)
end
if store_id == nil then
regex = data_type == 'form' and '[?&]store_id=([^&]+)' or '"store_id":"(%w+)"'
store_id = string.match(request_params, regex)
end
if client_os == nil then
regex = data_type == 'form' and '[?&]p10=([^&]+)' or '"p10":"(%w+)"'
client_os = string.match(request_params, regex)
end
if client_os_version == nil then
regex = data_type == 'form' and '[?&]p7=([^&]+)' or '"p7":"(%w+)"'
client_os_version = string.match(request_params, regex)
end
if client_type == nil then
regex = data_type == 'form' and '[?&]p9=([^&]+)' or '"p9":"(%w+)"'
client_type = string.match(request_params, regex)
end
if client_version == nil then
regex = data_type == 'form' and '[?&]p1=([^&]+)' or '"p1":"(%w+)"'
client_version = string.match(request_params, regex)
end
end
if client_os == nil and ngx.var.http_user_agent then
if string.find(ngx.var.http_user_agent, 'Mac OS X') then
client_os = 'ios'
client_os_version = string.match(ngx.var.http_user_agent, 'OS (%w+) like Mac OS X')
if client_os_version then
client_os_version = string.gsub(client_os_version, '_', '.')
end
elseif string.find(ngx.var.http_user_agent, 'Android') then
client_os = 'android'
client_os_version = string.match(ngx.var.http_user_agent, 'Android ([^;]+);')
end
end
if client_os then
client_os = string.lower(client_os)
end
if client_type then
client_type = string.lower(client_type)
end
if client_type == nil then
if ngx.var.http_user_agent and string.find(ngx.var.http_user_agent, 'MicroMessenger') then
client_type = 'wap'
else
client_type = 'app'
end
end
if ngx.var.http_cookie then
username = string.match(ngx.var.http_cookie, 'user_name=(%w+);?')
end
end
ngx.var.analysis = string.format('%s %s %s %s %s %s %s %s %s', access_token, user_id, user_key, username, store_id, client_os, client_os_version, client_type, ngx.var.http_host)