At work, the development of want to get data from Nginx POST log, first recorded
In the end of the log format plus $ request_body configuration information 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'; Add to print the log in server operation access_log logs/access.log main;
I thought the problem solved, the development required by adding data on the server response is returned in the log
The current nginx does not support the use of the output response packet body body_filter_by_lua
to the allocation request message body to a nginx
variable. The following is an example of
1: Download and install 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 Appears following successful compilation representation OK Successfully built LuaJIT make[1]: Leaving directory `/usr/local/src/LuaJIT-2.0.2/src' ==== Successfully built LuaJIT 2.0.2 ==== # make install The following appears to indicate a successful installation ==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2: Download ready nginx lua module
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: Install nginx
nginx-tar zxf 1.16.1.tar.gz cd nginx-1.16.1 // first import environment variable that tells where to find 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
Common error handling:
/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 Solution: # ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
4: Configure Nginx
nginx configuration file by adding the following configuration: location /test { default_type 'text/plain'; content_by_lua 'ngx.say("hello, ttlsa lua")'; }
5: Access test
curl http://127.0.0.1/test hello, ttlsa lua // test using curl, return the data indicate a successful installation
6: Nginx log configuration
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"';
Added in the appropriate configuration 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 ';
The following examples:
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; }
Start Nginx, print the log, you can see the relevant data.