解决疑难问题时,抓包是个必备的技能,我们开 accesslog 其实就是为了解决类似问题,但如果你 accesslog 没开呢?或者需要知道 body 中的内容呢?这个时候,goreplay 闪亮登场了。
下载 goreplay
下载地址: https://github.com/buger/goreplay/releases
请求转发
sudo nohup ./gor --http-original-host --input-raw :6802 \
--output-http="http://xxx.xxx.xxx.xxx:8080" > /dev/null 2>&1 &
如上,可将 6802 端口的请求全部转发到指定的 ip 和端口,这里的转发并不劫持该请求,即它是复制了一份请求然后转发,并不是 nginx 的 proxy_pass,不影响原请求的正常响应。
如果不想全量转发,可配合其 filter 使用,文档地址:https://github.com/buger/goreplay/wiki/Request-filtering
简单说,允许使用:–http-allow-url、–http-disallow-url、–http-allow-header、–http-disallow-header、–http-allow-method 来进行过滤,比如:
sudo nohup ./gor --http-original-host --input-raw :6802 \
--output-http="http://xxx.xxx.xxx.xxx:8080" --http-allow-method GET \
--http-allow-method POST > /dev/null 2>&1 &
记录原始请求 IP
可通过 --input-raw-realip-header "X-Real-IP"
将原始请求 ip 写入到 X-Real-IP header 中,举个例子:
sudo nohup ./gor --http-original-host --input-raw :6802 \
--input-raw-realip-header "X-Real-IP" \
--output-http="http://xxx.xxx.xxx.xxx:8080" \
--http-allow-method GET --http-allow-method POST > /dev/null 2>&1 &
评论