nginx日志中记录post数据

nginx记录post数据,需要安装lua-nginx-module模块

1、下载luajit: http://luajit.org/download.html

      make prefix=/usr/local/luajit

      make install prefix=/usr/local/luajit

2、下载ngx_devel_kit模块

      https://github.com/simpl/ngx_devel_kit/

解压到 /usr/local/src/ngx_devel_kit

3、下载lua-nginx-module模块

https://github.com/openresty/lua-nginx-module

解压到 /usr/local/src/lua_nginx_module

4、添加环境变量

exportLUAJIT_LIB=/usr/local/luajit/lib

exportLUAJIT_INC=/usr/local/luajit/include/luajit-2.0

5、nginx 编译

--user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/root/lnmp1.4-full/src/openssl-1.0.2l --add-module=/usr/local/src/ngx_devel_kit --add-module=/usr/local/src/lua-nginx-module

6、添加共享库

echo "/usr/local/luajit/lib" >> /etc/ld.so.conf

ldconfig

7、配置nginx

 在location /{}中添加

lua_need_request_body on;
        content_by_lua_block {
         ngx.req.read_body()
         local args, err = ngx.req.get_post_args(),ngstr,
         if not args then
             ngx.say("failed to get post args: ", err)
             return
         end
         for key, val in pairs(args) do
             if type(val) == "table" then
                 ngstr = '$ngstr - ngx.say(key, ": ", table.concat(val, ", "))'
             else
                 ngstr = '$ngstr - ngx.say(key, ": ", val)'
             end
             $request_body = ngstr
         end
        }

开启log access_log  /home/wwwlogs/XXX.log main;

http 中配置日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent $request_body "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


你可能感兴趣的:(linux)