听说你的Nginx还不会记录Response Body?

7,070 阅读4分钟

相信大家都遇到过在排查线上问题或Debug的时候,在某一瞬间,特别想开启Nginx的Response Body日志,来帮助自己快速的定位问题;但找半天发现只有request_body/upstream_addr/upstream_response_time这些相近变量可用;这个时候不要慌... 其实Nginx默认不开启Response Body的日志记录是有原因的,因为在Response Body过大,比如返回一个静态页面等内容时,不仅造成大量的资源占用,更会造成整个日志文件的可读性下降;但假如我们明确Response Body的格式,或想捕获Body里的关键字内容的时候,可以通过结合lua模块来实现这一需求。

Nginx编译安装

将Nginx和Lua结合可以使用OpenResty,也可以使用淘宝开源的Tengine,Tengine完全兼容Nginx;下面我们以Nginx编译lua模块为例:

我的系统环境:Ubuntu 14.04

# 先安装pcre库,Redhat系的机器对应的包可能为pcre-devel;也可以自己手动编译
aptitude -y install libpcre3-dev
# 下载并编译安装Lua解释器-LuaJIT
wget -c https://github.com/openresty/luajit2/archive/v2.1-20200102.tar.gz
tar -zxvf v2.1-20200102.tar.gz
cd ./luajit2-2.1-20200102
make && make install

# 告诉操作系统luajit的路径
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1
# 写进配置文件,为luajit配置动态链接库
echo '/usr/local/lib/' >> /etc/ld.so.conf.d/luajit_so.conf
ldconfig

# 下载并解压Nginx编译所需的模块
# OpenResty Lua 模块
wget -c wget -c https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
tar -zxvf v0.10.14.tar.gz

# ngx_devel_kit 模块
wget -c https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz
tar -zxvf v0.3.1.tar.gz

# 下载Nginx源码并编译安装
wget -c https://nginx.org/download/nginx-1.17.8.tar.gz
tar -zxvf nginx-1.17.8.tar.gz
cd nginx-1.17.8
# 简单编译示例
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.16rc5

make && make install

Nginx版本和模块信息查看

可以使用-V选项查看编译好的Nginx版本和模块信息

/usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.17.8
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.14

Nginx配置与lua的配置

下面是一个Nginx的简单配置示例,为使Nginx Conf的结构更清晰,我们使用include关键字,将log/lua/server几部分的配置拆分成单个独立的配置文件;下面看下每部分配置的示例:

Context HTTP示例

cd /usr/local/nginx/conf
cat nginx.conf

user  www www;
worker_processes  auto;

error_log  /data/nginx/logs/error.log warn;
pid        /data/nginx/nginx.pid;
events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    charset utf-8;
    # 日志格式配置文件
    include common/log_formats.conf;
    # 虚机主机配置
    include vhost/iyue_test.conf;

}

Context Server示例

这里我们用nginx代理GitHub的API接口来作为测试;自定义变量 resp_body(默认为空)用来存放Response Body的值,并通过lua功能模块来为其赋值。

cat vhost/iyue_test.conf

server {

    listen       80;
    server_name test.iyue.pub;

    proxy_ignore_client_abort on;
    fastcgi_ignore_client_abort on;

    root /data/htdocs/iyue;
    # 指定日志输出文件和日志格式为iyue_log
    access_log /data/nginx/logs/iyue_access.log iyue_log;
    # 自定义一个resp_body的变量,用来存放Response Body信息
    set $resp_body "";
    # 也可以使用body_filter_by_lua指令,将lua代码直接展示在这里;推荐采用文件的配置形式,方便后期维护
    body_filter_by_lua_file conf/lua/get_resp_code.lua;

    # 通过nginx配置代理github api,用于测试结果展示
    location /github/api/ {
        proxy_pass https://api.github.com/;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        #proxy_set_header Host $host;
        proxy_set_header Host api.github.com;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

lua代码示例

获取Respondy Body的值,并赋值给变量$resp_body:

cat lua/get_resp.lua

local chunk = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. chunk
if ngx.arg[2] then
    local resp = ngx.ctx.buffered
    ngx.var.resp_body = resp
end

这里ngx.arg[2]是一个bool类型;通过lua获取到整个Response Body的信息后,赋值给变量$resp_body;后面可以在log中打印该变量的值;

log_format示例

定义日志格式,并在log中打印自定义变量$resp_body;这里将其放到最后方便我们观察:

 cat common/log_formats.conf

log_format  iyue_log  '$remote_addr $http_x_forwarded_for [$time_local] $request_method $uri $args $request_body $server_protocol $status $body_bytes_sent $http_referer $http_user_agent $request_length rspt:$upstream_response_time rqtt:$request_time up:$upstream_addr $remote_port $host $resp_body';

结果展示

发起测试请求

curl http://test.iyue.pub/github/api/

从输出日志中,我们可以看到nginx已正常记录了Response Body的信息:

192.168.10.11 - [04/May/2020:17:35:55 +0800] GET /github/api/ - - HTTP/1.1 200 2178 - curl/7.54.0 88 rspt:0.884 rqtt:0.880 up:140.82.113.5:443 64502 test.iyue.pub {\x22current_user_url\x22:\x22https://api.github.com/user\x22,\x22current_user_authorizations_html_url\x22:\x22https://github.com/settings/connections/applications{/client_id}\x22,\x22authorizations_url\x22:\x22https://api.github.com/authorizations\x22,\x22code_search_url\x22:\x22https://api.github.com/search/code?q={query}{&page,per_page,sort,order}\x22,\x22commit_search_url\x22:\x22https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}\x22,\x22emails_url\x22:\x22https://api.github.com/user/emails\x22,\x22emojis_url\x22:\x22https://api.github.com/emojis\x22,\x22events_url\x22:\x22https://api.github.com/events\x22,\x22feeds_url\x22:\x22https://api.github.com/feeds\x22,\x22followers_url\x22:\x22https://api.github.com/user/followers\x22,\x22following_url\x22:\x22https://api.github.com/user/following{/target}\x22,\x22gists_url\x22:\x22https://api.github.com/gists{/gist_id}\x22,\x22hub_url\x22:\x22https://api.github.com/hub\x22,\x22issue_search_url\x22:\x22https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}\x22,\x22issues_url\x22:\x22https://api.github.com/issues\x22,\x22keys_url\x22:\x22https://api.github.com/user/keys\x22,\x22label_searc

Response Body过滤

通过上面的日志输出结果,我们看到日志中的带有'x22'的字样,这其实是双引号的16进制ASCII码;所以大家也能看出,这种打印出所有Response Body内容的方式,对特殊字符或中文输出不是很友好;这里建议的方案是,只输出你需要的Response Body中的某一片段,且输出结果尽量为英文;我们的lua代码可以进一步加下过滤输出,比如我们只输出匹配Response Body中'current_user_url'的结果,lua代码可变更为:

cat lua/get_resp.lua

local chunk = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. chunk
if ngx.arg[2] then
    local resp = ngx.ctx.buffered
    local user_url = ""
    user_url = string.match(resp, "\x22current_user_url\x22: \x22(.[^,]+)\x22")
    ngx.var.resp_body = user_url
end

优化后的日志输出结果为:

192.168.10.11 - [04/May/2020:18:05:40 +0800] GET /github/api/ - - HTTP/1.1 200 2308 - curl/7.54.0 88 rspt:0.900 rqtt:0.897 up:140.82.112.6:443 50340 test.iyue.pub https://api.github.com/user

可以看到日志格式更加整齐,且获取到了"current_user_url"对应的结果并正确打印。

可能会遇到的问题

在测试过程中如果你遇到如下报错:failed to load the 'resty.core' module,可能版本兼容性问题,可以尝试将lua版本降到0.10.14以下;

参考文档连接