nginx日志打印response返回内容

2020-08-10  本文已影响0人  GuanYZ

基于nginx打印返回值,此功能在nginx内置的功能中没有,需要安装第三方模块ngx_lua及相关很多模块,如果嫌麻烦且实际允许,建议直接卸载掉nginx使用openresty:

https://www.jianshu.com/p/a4f4f4282450

如果必须基于nginx来配置,请继续阅读 > > >

1. 安装LuaJIT

此模块需要Lua语言,所以需要安装相应的Lua语言包。下载可能很慢,耐心等待。

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

导入环境变量,告诉nginx去哪里找luajit

export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

2. 下载及解压nginx lua模块

作者是解压到/etc/nginx/modules文件夹下,读者按照实际要求,自定义文件夹

cd /etc/nginx/modules
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -zxvf v0.10.9rc7.tar.gz

3. 下载及解压nginx devel kit模块

cd /etc/nginx/modules
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz

4. nginx说明

安装nginx,如果是yum安装的,选要在下载nginx源码。否则无法将新增的模块编译到nginx中。

下载nginx源码需要跟已经安装好的nginx的版本对应上,示例:

nginx -v 
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/

5. 输入 nginx -V 命令,输入如下:

[root@localhost ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' 

其中“configure arguments:”之后的内容为当前nginx的模块配置参数,接下来需要将新增模块加入进去。

6.进入到nginx源码文件夹下,将新的模块编译进去

可以看到“./configure”后的参数是在之前的“configure arguments:”的基础上,追加了两个“--add-module”
执行如下命令:

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/etc/nginx/modules/ngx_devel_kit-0.2.19 --add-module=/etc/nginx/modules/lua-nginx-module-0.10.2

如果报错,看看报的什么错,作者报的错是少了两个组件:

yum -y install pcre-devel
yum -y install openssl openssl-devel

# 另外还少了一个配置
echo "/usr/local/luajit/lib" >> /etc/ld.so.conf

./configure 命令正确执行之后,重新编译nginx

make -j2
make install

然后重启ng

nginx -s stop
nginx

7. 配置nginx配置文件

修改nginx.conf,在log_format中添加response body的变量[ resp_body:"$resp_body"]

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

    access_log  /var/log/nginx/access.log  main;

然后server里面赋值:

server {
    listen       80;
    server_name  localhost;

    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 / {
    root /home/temp/;
        index index.html index.htm;
    }
}

重启ng,验证nginx日志

nginx -s reload

tail -f /var/log/nginx/access.log

输出内容为16进制的字符,需要转换一下才能看实际内容。转换方法略...

上一篇 下一篇

猜你喜欢

热点阅读