Adding request response logs to nginx logs
Get a new company., Do something new., After a day of mulling over, Finally added successfullyresponse journal
(located) atnginx Add the interface to the log of theresponse of the journal
Since this feature is available innginx Not among the built-in features, Requires installation of third-party modulesngx_lua, Since this module requiresLua language, So you need to install the appropriateLua language pack
1. Download and installLuaJIT
# cd /usr/local/src # 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
The following appears to indicate successful compilation 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, Indicates successful installation ==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2. Download preparationnginx lua module # cd /usr/local/src # wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz # tar -xzvf v0.8.6
3. erectnginx # cd /usr/local/src/ # wget http://nginx.org/download/nginx-1.4.2.tar.gz # tar -xzvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 // Introduce environment variables first, let knownginx Where to findluajit # export LUAJIT_LIB=/usr/local/lib # export LUAJIT_INC=/usr/local/include/luajit-2.0 # ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../lua-nginx-module-0.8.6 # make -j2 # make install
4. Test if the installation is successful
# cd /usr/local/nginx-1.4.2/conf/
# vi nginx.conf
lua command method
(located) atserver Add alocaltion
location /hello { default_type 'text/plain'; content_by_lua 'ngx.say("hello, lua")'; }
Then startnginx
# cd/usr/local/nginx-1.4.2/sbin
# ./nginx
Browser access:
http://127.0.0.1/hello
shows:hello, lua
point of attention:
1. Be careful not to make mistakes in the installation directory of each module
2. If previously passedapt-get Installed by way ofnginx, Need to delete firstnginx, Because the software downloaded in this way cannot be compiled
3.
This means that the third party installation was successful,
5. Start adding logs below
http { log_format mylog 'response_body:$resp_body'; server { # recordnginx Request Return Value 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:5000; access_log /var/log/nginx/access.log mylog; } } }
This has been added successfullyresponse journal。
If one understandsLua linguistic, The following code changes can be made, to be more in line with your own requirements。
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 ';
When logs are output, will find that the language response results involving character kanji are converted to16 The progression system causes unrecognizable。
Some of the response logs are as follows:
response_body: {x22codex22: 404, x22messagex22: x22xE8xAFxB7xE6xB1x82xE7x9Ax84xE8xB5x84xE6xBAx90xE4xB8x8DxE5xADx98xE5x9CxA8x22, x22datax22: {}}
This can be done by copying to thepython Script for resolution。
str1=''' {x22codex22: 404, x22messagex22: x22xE8xAFxB7xE6xB1x82xE7x9Ax84xE8xB5x84xE6xBAx90xE4xB8x8DxE5xADx98xE5x9CxA8x22, x22datax22: {}} ''' print(str1.encode('raw_unicode_escape').decode('utf-8'))
The input result is:
{"code": 404, "message": " The requested resource does not exist", "data": {}}
Reference URL:
https://www.cnblogs.com/aoeiuv/p/6856056.html
http://www.ttlsa.com/nginx/nginx-modules-ngx_lua/
https://blog.csdn.net/rona_lin/article/details/45028277