openresty+graylog 记录proxy 请求以及响应日志
openresty nginx 的增强版,同时基于灵活的生命周期阶段,我们可以做好多灵活的事情,以下就是一个
简单的处理请求以及响应log记录的,原本的打算是基于log_filter_by_lua 阶段处理的,后边发现直接使用
access_log 更简单省事,同时集成graylog灵活的log 处理能力,我们可以快速的实现proxy 请求以及响应
内容记录
环境准备
- nginx 配置
注意处理body 部分,我们需要使用ctx 进行数据存储方便读取response 数据(因为nginx 基于chunk 的数据传输)
同时我们需要使用escape=json (格式以及中文处理)
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
gzip on;
log_format graylog2_json escape=json '{ "timestamp": "$time_iso8601", '
'"remote_addr": "$remote_addr", '
'"body_bytes_sent": $body_bytes_sent, '
'"request_time": $request_time, '
'"response_status": $status, '
'"request": "$request", '
'"request_method": "$request_method", '
'"host": "$host",'
'"request_body":"$request_body",'
'"response_body":"$resp_body",'
'"upstream_cache_status": "$upstream_cache_status",'
'"upstream_addr": "$upstream_addr",'
'"http_x_forwarded_for": "$http_x_forwarded_for",'
'"http_referrer": "$http_referer", '
'"http_user_agent": "$http_user_agent" }';
real_ip_header X-Forwarded-For;
real_ip_recursive on;
upstream api {
server xxxx:port weight=20;
server xxx:port weight=10;
server xxxx:port weight=10;
}
server {
listen 80;
charset utf-8;
default_type text/html;
location / {
set $resp_body "";
access_log /usr/local/openresty/nginx/logs/access_test.log graylog2_json;
access_log syslog:server=graylog-server:12301 graylog2_json;
body_filter_by_lua_block {
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = string.sub((ngx.ctx.buffered or "") .. resp_body, 1, 1000)
-- arg[2] is true if this is the last chunk
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
}
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 120s;
proxy_connect_timeout 2s;
proxy_pass http://api;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- graylog content pack
graylog 已经提供了相关的content pack 我们只需要导入即可,同时会创建相关的input,stream,rules,dashboard
可以参考https://github.com/petestorey26/graylog-content-pack-nginx-json
一些说明
我们最好是对于不同的log提供不同的索引(可以基于规则处理数据存储),这样可以减少一些问题,同时对于openresty
的body_filter 需要做一些处理才能准确的获取response数据
参考资料
https://github.com/petestorey26/graylog-content-pack-nginx-json
https://nginx.org/en/docs/syslog.html
https://docs.graylog.org/en/3.3/
https://docs.graylog.org/en/3.3/pages/content_packs.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2019-06-09 python 微服务开发书中几个方便的python框架
2016-06-09 Swagger-UI 基于REST的API测试/文档类插件
2015-06-09 webStorm 注册码 (备用)