在工作中,开发希望能从Nginx日志中获取POST的数据信息,先记录下来
在日志格式后面加上 $request_body 配置信息 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'; 在server中添加打印日志的操作 access_log logs/access.log main;
本以为问题解决了,开发有要求在日志中添加上 服务器响应返回的数据
目前的 nginx 是不支持输出 response 报文体的 使用body_filter_by_lua
来分配请求报文体给一个nginx
变量。下面是一个示例
1:下载安装LuaJIT
# 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 出现如下内容表示编译成功 OK Successfully built LuaJIT make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src' ==== Successfully built LuaJIT 2.0.2 ==== # make install 出现如下内容,表示安装成功 ==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2:下载准备nginx lua模块
wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz tar -xzvf lua-nginx-module-0.8.6.tar.gz mv lua-nginx-module-0.8.6 /usr/local/src/lua-nginx-module-0.8.6
3:安装nginx
tar zxf nginx-1.16.1.tar.gz cd nginx-1.16.1 //先导入环境变量,告诉nginx去哪里找luajit export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.0 ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/lua-nginx-module-0.8.6 --with-http_ssl_module --with-http_stub_status_module --with-pcre make j2 make install
常见错误处理:
/usr/local/nginx-1.4.2/sbin/nginx -v ./objs/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory 解决方法: # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
4:配置Nginx
nginx配置文件加入如下配置: location /test { default_type 'text/plain'; content_by_lua 'ngx.say("hello, ttlsa lua")'; }
5:访问测试
curl http://127.0.0.1/test hello, ttlsa lua //使用curl测试,返回数据表示安装成功
6: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 resp_body:"$resp_body"';
在server中添加相应的配置:
lua_need_request_body on; set $resp_body ""; body_filter_by_lua ' local resp_body = string.sub(ngx.arg[1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end ';
如下事例:
server { listen 443 ssl; server_name xxxx.com; # ssl on; ssl_certificate /usr/local/nginx/conf/keys/public.pem; ssl_certificate_key /usr/local/nginx/conf/keys/private.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!3DES:!aNULL:!eNULL; ssl_prefer_server_ciphers on; lua_need_request_body on; set $resp_body ""; body_filter_by_lua ' local resp_body = string.sub(ngx.arg[1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body if ngx.arg[2] then ngx.var.resp_body = ngx.ctx.buffered end '; location / { proxy_pass http://127.0.0.1:8080; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header User-Agent $http_user_agent; proxy_set_header Referer $http_referer; client_max_body_size 50m; client_body_buffer_size 256k; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; proxy_buffer_size 512k; proxy_buffers 16 512k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 512k; } access_log /tmp/faceauth.log main; }
启动Nginx,打印日志,就可以看到相关数据。
以上,是打印POST数据的一种方法,比较麻烦,下面介绍一种比较简单的方法:
在日志中添加“$request_body”,由于现实原因,需要添加 escape=json 转换为json格式
log_format access escape=json '$remote_addr - $remote_user [$time_local] "$request" - "$request_body" ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$upstream_addr" "$upstream_status" "$request_time" "$upstream_response_time" $bytes_sent $request_length';