一般我们在开发过程中,尤其是和另外一个系统联调的时候,接口无法获取正常的参数,而抓包又异常困难的话,从 nginx 入手,不失为一个好的思路。
0x00. 准备nginx 的变量$request_body 即为http请求的body数据
只有在 location中 有 proxy_pass,fastcgi_pass,scgi_pass命令存在时,$request_body变量才会有值。
反向代理使用 proxy_pass
PHP 服务使用 fastcgi_pass
0x01 配置
1. 原理:
定义一个专门接收body数据的日志格式,然后在带有proxy_pass,fastcgi_pass,scgi_pass的 location 中添加 log日志
2. 定义日志格式的命令:log_format ;
可能会用到以下nginx变量参数 说明 示例
$remote_addr 客户端地址 211.28.65.253
$remote_user 客户端用户名称 --
$time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
$request 请求的URI和HTTP协议 "GET / HTTP/1.1"
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.baidu.com 192.168.100.100
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 1547
$http_referer url跳转来源 https://www.baidu.com/
$http_user_agent 用户终端浏览器等信息 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80
$request_time 整个请求的总时间 0.205
$upstream_response_time 请求过程中,upstream响应时间 0.002
3. 添加日志
命令:access_log ;
注:此命令需要在 带有proxy_pass,fastcgi_pass,scgi_pass的 location 中添加,否则无法获取到http请求的body值
0x02 完整配置示例http {
log_format body_logs '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent body:"$request_body" "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
location /api {
fastcgi_pass 127.0.0.1:9000;
access_log /your/logs/path/post_body.log body_logs;
}
}
}