1. 程式人生 > >nginx日誌中記錄post資料

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模組

解壓到 /usr/local/src/ngx_devel_kit

3、下載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"';