当前位置:首页 > 技术文章 > 正文内容

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

arlanguage5个月前 (12-13)技术文章46

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

## Nginx编译安装

将Nginx和Lua结合可以使用[OpenResty](https://openresty.org/cn/),也可以使用淘宝开源的[Tengine](http://tengine.taobao.org/documentation_cn.html),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以下;

## 符参考文档连接

* Nginx: https://nginx.org/

* Tengine: http://tengine.taobao.org/documentation_cn.html

* OpenResty Lua: https://github.com/openresty/lua-nginx-module#readme

* GitHub API v3: https://developer.github.com/v3/

* Nginx源码下载: https://nginx.org/download/

* ngx_lua模块下载: https://github.com/openresty/lua-nginx-module/tags

* LuaJIT下载: https://github.com/openresty/luajit2/releases

* ngx_devel_kit(NDK)模块下载: https://github.com/vision5/ngx_devel_kit/tags

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/144.html

标签: nginx的logs
分享给朋友:

“听说你的Nginx还不会记录Response Body?” 的相关文章

分享一段PHP代码的加密扩展 分享一段php代码的加密扩展怎么弄

介绍一个简洁、高性能、跨平台的 PHP7 代码加密扩展特点简单快速,经实测,几乎不影响性能兼容 OPcache、Xdebug 等其他扩展支持 Linux、macOS、Windows 等系统兼容 Apache、Nginx + PHP-fpm、命令行等运行模式加密算法较简单,这是出于速度考虑,但仍不易解...

Python WSGI 高性能服务器 - Gunicorn 简介

一、简介Gunicorn 意即 Green Unicorn,绿色独角兽。它是一个被广泛使用的高性能的 Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目。它具有如下特性:原生支持 WSGI、Django 和 Paster自动工作进程管理简单的 Pytho...

Nginx常用配置及和基本功能讲解

作者:京东物流 殷世杰Nginx已经广泛应用于J-one和Jdos的环境部署上,本文对Nginx的常用的配置和基本功能进行讲解,适合Ngnix入门学习。1 核心配置找到Nginx安装目录下的conf目录下nginx.conf文件,Nginx的基本功能配置是由它提供的。1.1 配置文件结构Nginx的...

利用Nginx正向代理实现局域网电脑访问外网

引言在网络环境中,有时候我们需要让局域网内的电脑访问外网,但是由于网络策略或其他原因,直接访问外网是不可行的。这时候,可以借助 Nginx 来搭建一个正向代理服务器,实现局域网内电脑通过 Nginx 转发访问外网的需求。在工作中我遇到了一个类似的情况:在公司网络中,由于管理要求,局域网内的电脑不能直...

nginx配置静态资源服务器 nginx配置静态资源服务器的方法

nginx服务器搭建在这就不过多的说明了,本内容主要是nginx静态资源服务器配置相关说明nginx配置location / :指所有的url请求都会去访问static下面的文件;指定url后缀要与文件目录后面的后缀一一对应有两种用法:root:绝对路径;alias: nginx安装目录中的stat...

基于一次ContentDownload变慢而进行的网站性能优化

平时运行无常的网站,突然出现了某个接口TTFB缓慢问题,于是乎开始定位和解决,先说明一下网站的技术栈: 编程语言:PHP、Javascript、Vue、Java 开发框架:Laravel、SpringBoot 中间件:Redis、Kafka、Nginx 数据库:Mysql、MongoDB 云服务:腾...