请求body添加:
在nginx配置文件log_format添加request_body:"$request_body"
响应body添加:
需要借助lua模块
1.下载安装LuaJIT
cd /usr/local/src
wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
tar -xzvf LuaJIT-2.0.5.tar.gz
cd LuaJIT-2.0.5
make
make install
出现如下内容,表示安装成功
==== Successfully installed LuaJIT 2.0.2 to /usr/local ====
2.下载nginx lua模块
cd /usr/local/src
wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.10.15.tar.gz
tar -xzvf v0.10.15.tar.gz
3.nginx添加lua模块
-
a.查看原来的编译参数
[root@iZwz9b8dyz1jpybdicn3lcZ nginx]# ./sbin/nginx -V
nginx version: nginx/1.15.8
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/wdlinux/nginx-1.15.8 --with-http_stub_status_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_ssl_module
-
b.从新编译,添加模块
cd /usr/local/src/nginx
./configure --user=www --group=www --prefix=/www/wdlinux/nginx-1.15.8 \
--with-http_stub_status_module --with-ipv6 --with-http_gzip_static_module \
--with-http_realip_module --with-http_ssl_module --add-module=/usr/local/src/lua-nginx-module-0.10.15
make
-
c.make编译完后不要执行make install 在objs下面有一个nginx执行文件
-
d.备份原来nginx可执行文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
-
e.吧新的nginx文件copy到原来nginx运行sbin目录下
cp objs/nginx /usr/local/nginx/sbin/nginx
-
f.测试查看编译参数
/usr/local/nginx/sbin/nginx -t
-
g.执行make upgrade替换老的nginx进程
make upgrade
-
h.再次执行nginx -V将会显示新的nginx的版本及编译的参数
4.测试lua是否安装成功
- vi nginx.conf在server 中添加一个localtion
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
- 重启nginx
/usr/local/nginx/sbin/nginx -s reload
访问url会返回hello, lua则表示成功
5.修改nginx.conf文件增加返回响应body
-
a.修改log_format添加resp_body
log_format api '$remote_addr - requesttime:"$request_time" $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "user_token": "$http_authorization"'
'request_body:"$request_body" resp_body:"$resp_body" ' ;
-
b.server添加响应内容,然后重启nginx
server {
access_log /var/log/nginx/access.log api;
#记录nginx请求返回值
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;
}
}
日志响应内容如下
222.209.88.67 - requesttime:"0.008" - [11/Jun/2019:10:08:20 +0800] "GET /User/Seneschal/Get?userId=1137965292712820737 HTTP/1.1" 200 474 "https://servicewechat.com/wxc88116904036wced89/devtools/page-frame.html" "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 wechatdevtools/1.02.1904091 MicroMessenger/6.7.3 Language/zh_CN webview/" "-" "user_token": "Bearer E955ED0FD1EC43AD87A9134F06A9068D"request_body:"-" resp_body:"{\x22data\x22:{\x22workUrl\x22:\x22http://testimg.xxxx.com/Seneschal/1137965292712820737/WorkCode/1137965293488037888.jpg\x22,\x22storeName\x22:\x22\xE6\x98\x93\xE8\xB4\xA4\xE9\x9C\x96test1\x22,\x22createDate\x22:1560147042813,\x22memberQuantity\x22:1,\x22brokerage\x22:0.00,\x22consume\x22:0.00,\x22channelBrokerage\x22:0.00,\x22serviceBrokerage\x22:0.00,\x22cashAmount\x22:0.00,\x22state\x22:1,\x22id\x22:\x221137965292712820737\x22,\x22userName\x22:\x22\xE5\x8D\x8E\xE7\x9D\x80\xE7\x82\xB9\x22,\x22avatar\x22:\x22\x22,\x22account\x22:\x2216666666668\x22,\x22channelCommission\x22:0.010,\x22serviceCommission\x22:0.010},\x22errorCode\x22:0,\x22message\x22:\x22\xE6\x89\xA7\xE8\xA1\x8C\xE6\x88\x90\xE5\x8A\x9F\x22}"
使用python解码对应的req body或者resp body
str1='''
{\x22data\x22:{\x22workUrl\x22:\x22http://testimg.xxxx.com/Seneschal/1137965292712820737/WorkCode/1137965293488037888.jpg\x22,\x22storeName\x22:\x22\xE6\x98\x93\xE8\xB4\xA4\xE9\x9C\x96test1\x22,\x22createDate\x22:1560147042813,\x22memberQuantity\x22:1,\x22brokerage\x22:0.00,\x22consume\x22:0.00,\x22channelBrokerage\x22:0.00,\x22serviceBrokerage\x22:0.00,\x22cashAmount\x22:0.00,\x22state\x22:1,\x22id\x22:\x221137965292712820737\x22,\x22userName\x22:\x22\xE5\x8D\x8E\xE7\x9D\x80\xE7\x82\xB9\x22,\x22avatar\x22:\x22\x22,\x22account\x22:\x2216666666668\x22,\x22channelCommission\x22:0.010,\x22serviceCommission\x22:0.010},\x22errorCode\x22:0,\x22message\x22:\x22\xE6\x89\xA7\xE8\xA1\x8C\xE6\x88\x90\xE5\x8A\x9F\x22}
'''
print(str1.encode('raw_unicode_escape').decode('utf-8'))
网友评论