百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

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

yuyutoo 2024-11-10 13:48 5 浏览 0 评论

相信大家都遇到过在排查线上问题或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

相关推荐

TCP协议原理,有这一篇就够了

先亮出这篇文章的思维导图:TCP作为传输层的协议,是一个软件工程师素养的体现,也是面试中经常被问到的知识点。在此,我将TCP核心的一些问题梳理了一下,希望能帮到各位。001.能不能说一说TC...

Win10专业版无线网络老是掉线的问题

有一位电脑基地的用户,使用...

学习计算机网络需要掌握以下几方面基础知识

计算机基础知识操作系统:了解常见操作系统(如Windows、Linux)的基本操作和网络配置,例如如何设置IP地址、子网掩码、网关和DNS服务器等,以及如何通过命令行工具(如ping、tr...

网络工程师的圣经!世界级网工手绘268张图让TCP/IP直接通俗易懂

要把知识通俗地讲明白,真的不容易。——读者说TCP/IP从字面意义上讲,有人可能会认为TCP/IP是指TCP和IP两种协议。实际生活当中有时候也确实就是这两种协议。然而在很多情况下,它只是...

三分钟了解通信知识TCP与IP协议(含“通信技术”资料分享)

TCP/IPTCP/IP分层模型①应用层...

网闸与防火墙:网络安全设备的差异与应用

在网络安全领域,网闸(安全隔离网闸,GAP)和防火墙(Firewall)是两类重要的防护设备。尽管它们都服务于网络安全防护,但在设计理念、技术原理、安全效能及适用场景等方面存在显著差异,以下从五个维度...

S7-300的TCP/IP通信

一、首先在项目中创建2个S7-300的站点;二、硬件组态中,设置合适的TCP/IP地址,在同一网段内;...

西门子S7-1500 PLC的 MODBUS TCP通信

MODBUSTCP使MODBUS_RTU协议运行于以太网,MODBUSTCP使用TCP/IP和以太网在站点间传送MODBUS报文,MODBUSTCP结合了以太网物理网络和网络标准TC...

系统规划与管理师新版备考必备:第7章考点思维导图解析

备考系统规划与管理师的小伙伴们,福利又来啦!今天为大家带来《系统规划与管理师(第2版)》第7章考点的思维导图,助你高效梳理重点,让备考更有方向!...

TCP/IP、Http、Socket 有何区别与联系?

HTTP协议对应于应用层,Socket则是对TCP/IP协议的封装和应用(程序员层面上)。HTTP是应用层协议,主要解决如何包装数据。而我们平时说的最多的Socket是什么呢?实际上...

西门子PLC串口协议与以太网通信协议对比

西门子plc品牌众多,通信协议的类型就更多了,具体可分为串口协议和以太网通信协议两大类。...

网络编程懒人入门(十三):一泡尿的时间,快速搞懂TCP和UDP的区别

本文引用了作者Fundebug的“一文搞懂TCP与UDP的区别”一文的内容,感谢无私分享。1、引言...

程序员必备的学习笔记《TCP/IP详解(一)》

为什么会有TCP/IP协议在世界上各地,各种各样的电脑运行着各自不同的操作系统为大家服务,这些电脑在表达同一种信息的时候所使用的方法是千差万别。就好像圣经中上帝打乱了各地人的口音,让他们无法合作一样...

一文读懂TCP/IP协议工作原理和工作流程

简述本文主要介绍TCP/IP协议工作原理和工作流程。含义TCP/IP协议,英文全称TransmissionControlProtocol/InternetProtocol,包含了一系列构成互联网...

如何在 Windows 10 和 Windows 11 上重置 TCP/IP 堆栈

传输控制协议/Internet协议,通常称为TCP/IP,是您的WindowsPC如何与Internet上的其他设备进行通信的关键部分。但是当事情出错时会发生什么?你如何解决它?幸运的...

取消回复欢迎 发表评论: