您当前的位置:首页 > Linux 网站首页Linux
使用ngx_lua记录nginx的Response
大家都知道,如果要记录用户访问web服务的请求信息,可以使用nginx的log功能,配置access log即可记录访客的客户端信息,请求信息 查看 nginx的变量。或者使用httpry监听80端口的web请求。
但是当用户请求时返回的内容nginx日志是无法记录的,特别的应用场景是调试接口数据请求与返回。例如对支付宝、微信接口进行调试的时候看不到对方服务器请求之后返回的数据正确与否。此时nginx就要借nginx的lua插件来截获response信息作为nginx的变量进行记录。
其实很希望官方能够升级在变量列表里增加response_bod,当然自己修改nginx源码,然后再编译也是另外一种方法。后面有机会再介绍。
进入正题,本次需要安装
· luajit 地址:http://luajit.org/download.html
· nginx 地址:http://www.nginx.org
· HttpLuaModule 地址:http://wiki.nginx.org/HttpLuaModule
1. 下载安装LuaJIT
# cd /tmp
# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
# tar -xzvf LuaJIT-2.0.2.tar.gz
# cd LuaJIT-2.0.2
# make
==== Building LuaJIT 2.0.2 ====
make -C src
make[1]: Entering directory `/tmp/LuaJIT-2.0.2/src'
HOSTCC host/minilua.o
HOSTLINK host/minilua
DYNASM host/buildvm_arch.h
HOSTCC host/buildvm.o
HOSTCC host/buildvm_asm.o
HOSTCC host/buildvm_peobj.o
HOSTCC host/buildvm_lib.o
HOSTCC host/buildvm_fold.o
HOSTLINK host/buildvm
BUILDVM lj_vm.s
ASM lj_vm.o
CC lj_gc.o
BUILDVM lj_ffdef.h
CC lj_err.o
CC lj_char.o
BUILDVM lj_bcdef.h
CC lj_bc.o
CC lj_obj.o
CC lj_str.o
CC lj_tab.o
CC lj_func.o
CC lj_udata.o
CC lj_meta.o
CC lj_debug.o
CC lj_state.o
CC lj_dispatch.o
CC lj_vmevent.o
CC lj_vmmath.o
CC lj_strscan.o
CC lj_api.o
CC lj_lex.o
CC lj_parse.o
CC lj_bcread.o
CC lj_bcwrite.o
CC lj_load.o
CC lj_ir.o
CC lj_opt_mem.o
BUILDVM lj_folddef.h
CC lj_opt_fold.o
CC lj_opt_narrow.o
CC lj_opt_dce.o
CC lj_opt_loop.o
CC lj_opt_split.o
CC lj_opt_sink.o
CC lj_mcode.o
CC lj_snap.o
CC lj_record.o
CC lj_crecord.o
BUILDVM lj_recdef.h
CC lj_ffrecord.o
CC lj_asm.o
CC lj_trace.o
CC lj_gdbjit.o
CC lj_ctype.o
CC lj_cdata.o
CC lj_cconv.o
CC lj_ccall.o
CC lj_ccallback.o
CC lj_carith.o
CC lj_clib.o
CC lj_cparse.o
CC lj_lib.o
CC lj_alloc.o
CC lib_aux.o
BUILDVM lj_libdef.h
CC lib_base.o
CC lib_math.o
CC lib_bit.o
CC lib_string.o
CC lib_table.o
CC lib_io.o
CC lib_os.o
CC lib_package.o
CC lib_debug.o
CC lib_jit.o
CC lib_ffi.o
CC lib_init.o
AR libluajit.a
CC luajit.o
BUILDVM jit/vmdef.lua
DYNLINK libluajit.so
LINK luajit
OK Successfully built LuaJIT
make[1]: Leaving directory `/tmp/LuaJIT-2.0.2/src'
==== Successfully built LuaJIT 2.0.2 ====
make install
...
...
ln -sf luajit-2.0.2 /usr/local/bin/luajit
==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
至此LuaJIT安装完毕。即使是在Ubuntu上安装,LuaJIT 2.0也是推荐的方式。
Installation on Ubuntu 11.10
Note that it is recommended to use LuaJIT 2.0 or LuaJIT 2.1 instead of the standard Lua 5.1 interpreter wherever possible.
If the standard Lua 5.1 interpreter is required however, run the following command to install it from the Ubuntu repository:
apt-get install -y lua5.1 liblua5.1-0 liblua5.1-0-dev
Everything should be installed correctly, except for one small tweak.
Library name liblua.so
has been changed in liblua5.1 package, it only comes with liblua5.1.so
, which needs to be symlinked to /usr/lib
so it could be found during the configuration process.
ln -s /usr/lib/x86_64-linux-gnu/liblua5.1.so /usr/lib/liblua.so
2. 下载准备nginx lua模块
# cd /tmp
# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc6.tar.gz
# tar -xzvf v0.10.9rc6.tar.gz
关于下载nginx lua模块的版本与nginx core版本的兼容关系这里有说明。
Nginx Compatibility
The latest version of this module is compatible with the following versions of Nginx:
- 1.11.x (last tested: 1.11.2)
- 1.10.x
- 1.9.x (last tested: 1.9.15)
- 1.8.x
- 1.7.x (last tested: 1.7.10)
- 1.6.x
Nginx cores older than 1.6.0 (exclusive) are not supported.
3. 下载安装nginx
3.1 下载安装
wget 'http://nginx.org/download/nginx-1.11.2.tar.gz'
tar -xzvf nginx-1.11.2.tar.gz
cd nginx-1.11.2/
# tell nginx's build system where to find LuaJIT 2.0:
export LUAJIT_LIB=/path/to/luajit/lib
export LUAJIT_INC=/path/to/luajit/include/luajit-2.0
# tell nginx's build system where to find LuaJIT 2.1:
export LUAJIT_LIB=/path/to/luajit/lib
export LUAJIT_INC=/path/to/luajit/include/luajit-2.1
# or tell where to find Lua if using Lua instead:
#export LUA_LIB=/path/to/lua/lib
#export LUA_INC=/path/to/lua/include
此处我采用的是
/tmp/nginx-1.11.2# export LUAJIT_LIB=/usr/local/lib
/tmp/nginx-1.11.2# export LUAJIT_INC=/usr/local/include/luajit-2.0
/tmp/nginx-1.11.2# mv ../lua-nginx-module-0.10.9rc6 lua-nginx-module-0.10.9
/tmp/nginx-1.11.2# ./configure --prefix=/usr/local/nginx-1.11.2 --add-module=lua-nginx-module-0.10.9
...
...
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx-1.11.2"
nginx binary file: "/usr/local/nginx-1.11.2/sbin/nginx"
nginx modules path: "/usr/local/nginx-1.11.2/modules"
nginx configuration prefix: "/usr/local/nginx-1.11.2/conf"
nginx configuration file: "/usr/local/nginx-1.11.2/conf/nginx.conf"
nginx pid file: "/usr/local/nginx-1.11.2/logs/nginx.pid"
nginx error log file: "/usr/local/nginx-1.11.2/logs/error.log"
nginx http access log file: "/usr/local/nginx-1.11.2/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
make && make install
安装完成之后可执行查看nginx版本
# /usr/local/nginx-1.11.2/sbin/nginx -v
nginx version: nginx/1.11.2
更多configure参数参考:
--prefix=/usr/local/nginx-1.11.2 --add-module=lua-nginx-module-0.10.9 --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.2.6/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.2.6/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.2.6/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.2.6/debian/modules/nginx-dav-ext-module
nginx配置
log_format main '$remote_addr | $remote_user | [$time_local] | "$request" | '
'$status | $body_bytes_sent | "$http_referer" | '
'"$http_user_agent" | "$http_x_forwarded_for" | "$request_body" | "$response_body"';
server {
... ...
access_log /var/log/nginx/api.xiniutaoke.com.access.log main;
set $response_body "";
location ~ \.php$ {
lua_need_request_body on;
body_filter_by_lua '
local response_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or"") .. response_body
if ngx.arg[2] then
ngx.var.response_body = ngx.ctx.buffered
end
';
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
... ...
}
我的日志示例如下
58.56.46.** - [27/Jun/2017:16:50:19 +0800] [200] - api.xxx.com GET /api/ HTTP/1.1 - RESPONSE:{'code':200010,'msg':'\xE6\x9C\xAA\xE8\x8E\xB7\xE5\x8F\x96\xE5\x88\xB0\xE7\x94\xA8\xE6\x88\xB7\xE7\x9A\x84id\xE4\xBF\xA1\xE6\x81\xAF'}
那么这个记录的日志还是不能很好的被识别,因为内容被转换成为了16进制。那么
转载请注明出处:黄桂林的博客关键字: nginx / lua / log / nginx响应日志 / nginx response /