nginx 记录完整的 request 及 response

前言

在开发的过程中, 经常会有抓包的需求, 查看请求体和响应体. 使用 charles 等抓包工具会遇到一些麻烦, 如:

  1. localhost 请求无法捕获
  2. 有些工具配置代理比较麻烦, 如docker配置代理后需要重启
  3. https协议需要代理端配置证书进行解密, 比较麻烦

于是, 我就在想, 能否直接在服务端将所有的请求体和响应体打印出来, 不就完美解决这个问题了么?

一般来说, 通过nginx代理请求, 所有的请求都过nginx, nginx自身也有https的私钥, 会进行解密.

那么问题来了, 如何通过nginx打印请求呢?

中间探索的过程就不提了, 直接上结果.

lua打印

这里直接使用openresty/openresty镜像了, 当然如果直接使用nginx编译lua插件也是可以的.

在配置文件中添加如下lua脚本以打印完整请求内容:

lua 复制代码
# 将请求信息暂存, 放到最后一起打印
access_by_lua_block {
  ngx.req.read_body()
  local req_body = ngx.req.get_body_data()
  local req_uri = ngx.var.request_uri
  if req_body and #req_body > 1024 * 1024 then
    req_body = "Request too large"
  end
  if not req_body then
    req_body = "No request body"
  end
  local headers = ngx.req.get_headers()
  local header_str = ""
  for k, v in pairs(headers) do
    header_str = header_str .. string.format("%s: %s\n", k, v)
  end
  ngx.ctx.req_info = {
    uri = req_uri,
    body = req_body,
    headers = header_str
  }
}

body_filter_by_lua_block {
  local resp_body = ngx.arg[1]
  local req_info = ngx.ctx.req_info or {}
  local req_uri = req_info.uri or "Unknown URI"
  local req_body = req_info.body or "Unknown Request Body"
  local req_headers = req_info.headers or "Unknown Headers"
  local resp_headers = ngx.resp.get_headers()
  local resp_header_str = ""
  for k, v in pairs(resp_headers) do
    resp_header_str = resp_header_str .. string.format("%s: %s\n", k, v)
  end
  ngx.log(ngx.ERR, string.format(
      "Request URI: %s\nRequest Headers:\n%sRequest Body: %s\nResponse Headers:\n%sResponse Body: %s",
      req_uri,
      req_headers,
      req_body,
      resp_header_str,
      resp_body or "No response body"
    ))
}

这段逻辑在location中.

完整内容可参考: https://github.com/hujingnb/docker_composer/tree/master/openresty

简单记录一下, 以方便后续获取请求使用

相关推荐
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu3 小时前
Spring MVC消息转换器
java·spring
乔冠宇3 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ4 小时前
简单的SQL语句的快速复习
java·数据库·sql
komo莫莫da5 小时前
寒假刷题Day19
java·开发语言
S-X-S5 小时前
算法总结-数组/字符串
java·数据结构·算法
linwq85 小时前
设计模式学习(二)
java·学习·设计模式
桦说编程6 小时前
CompletableFuture 超时功能有大坑!使用不当直接生产事故!
java·性能优化·函数式编程·并发编程
@_@哆啦A梦6 小时前
Redis 基础命令
java·数据库·redis
千夜啊7 小时前
Nginx 运维开发高频面试题详解
运维·nginx·运维开发