听说你的Nginx还不会记录Response Body?
yuyutoo 2024-11-10 13:48 3 浏览 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
相关推荐
- 史上最全的浏览器兼容性问题和解决方案
-
微信ID:WEB_wysj(点击关注)◎◎◎◎◎◎◎◎◎一┳═┻︻▄(页底留言开放,欢迎来吐槽)●●●...
-
- 平面设计基础知识_平面设计基础知识实验收获与总结
-
CSS构造颜色,背景与图像1.使用span更好的控制文本中局部区域的文本:文本;2.使用display属性提供区块转变:display:inline(是内联的...
-
2025-02-21 16:01 yuyutoo
- 写作排版简单三步就行-工具篇_作文排版模板
-
和我们工作中日常word排版内部交流不同,这篇教程介绍的写作排版主要是用于“微信公众号、头条号”网络展示。写作展现的是我的思考,排版是让写作在网格上更好地展现。在写作上花费时间是有累积复利优势的,在排...
- 写一个2048的游戏_2048小游戏功能实现
-
1.创建HTML文件1.打开一个文本编辑器,例如Notepad++、SublimeText、VisualStudioCode等。2.将以下HTML代码复制并粘贴到文本编辑器中:html...
- 今天你穿“短袖”了吗?青岛最高23℃!接下来几天气温更刺激……
-
最近的天气暖和得让很多小伙伴们喊“热”!!! 昨天的气温到底升得有多高呢?你家有没有榜上有名?...
- CSS不规则卡片,纯CSS制作优惠券样式,CSS实现锯齿样式
-
之前也有写过CSS优惠券样式《CSS3径向渐变实现优惠券波浪造型》,这次再来温习一遍,并且将更为详细的讲解,从布局到具体样式说明,最后定义CSS变量,自定义主题颜色。布局...
- 你的自我界限够强大吗?_你的自我界限够强大吗英文
-
我的结果:A、该设立新的界限...
- 行内元素与块级元素,以及区别_行内元素和块级元素有什么区别?
-
行内元素与块级元素首先,CSS规范规定,每个元素都有display属性,确定该元素的类型,每个元素都有默认的display值,分别为块级(block)、行内(inline)。块级元素:(以下列举比较常...
-
- 让“成都速度”跑得潇潇洒洒,地上地下共享轨交繁华
-
去年的两会期间,习近平总书记在参加人大会议四川代表团审议时,对治蜀兴川提出了明确要求,指明了前行方向,并带来了“祝四川人民的生活越来越安逸”的美好祝福。又是一年...
-
2025-02-21 16:00 yuyutoo
- 今年国家综合性消防救援队伍计划招录消防员15000名
-
记者24日从应急管理部获悉,国家综合性消防救援队伍2023年消防员招录工作已正式启动。今年共计划招录消防员15000名,其中高校应届毕业生5000名、退役士兵5000名、社会青年5000名。本次招录的...
- 一起盘点最新 Chrome v133 的5大主流特性 ?
-
1.CSS的高级attr()方法CSSattr()函数是CSSLevel5中用于检索DOM元素的属性值并将其用于CSS属性值,类似于var()函数替换自定义属性值的方式。...
- 竞走团体世锦赛5月太仓举行 世界冠军杨家玉担任形象大使
-
style="text-align:center;"data-mce-style="text-align:...
- 学物理能做什么?_学物理能做什么 卢昌海
-
作者:曹则贤中国科学院物理研究所原标题:《物理学:ASourceofPowerforMan》在2006年中央电视台《对话》栏目的某期节目中,主持人问过我一个的问题:“学物理的人,如果日后不...
-
- 你不知道的关于这只眯眼兔的6个小秘密
-
在你们忙着给熊本君做表情包的时候,要知道,最先在网络上引起轰动的可是这只脸上只有两条缝的兔子——兔斯基。今年,它更是迎来了自己的10岁生日。①关于德艺双馨“老艺...
-
2025-02-21 16:00 yuyutoo
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)