nginx mirror/post_action+gor实现https流量复制
个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/11850386.html
关于gor:
参考:
https://www.cnblogs.com/jinjiangongzuoshi/p/11773070.html
https://github.com/buger/goreplay/wiki/Getting-Started
这篇小记主要是记录gor不支持https流量镜像的一种解决思路
https流量镜像
1、在nginx proxy 配置 post_action(或者mirror),实现https流量镜像出http流量到gor
server
{
listen 80;
listen 443;
server_name tt.example.com;
index index.html index.php;
root /data/web/webclose;
location / {
expires off;
proxy_redirect off;
proxy_set_header Host tt.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://1.1.1.1:443;
#配置post_action
post_action @tt_http;
#或者配置mirror
mirror /mirror;
mirror_request_body on; #指定是否镜像请求body部分,此选项与proxy_request_buffering、fastcgi_request_buffering、scgi_request_buffering和 uwsgi_request_buffering冲突,一旦开启mirror_request_body,则请求自动缓存;
}
#mirror
location /mirror {
internal; #指定此location只能被“内部的”请求调用,外部的调用请求会返回”Not found” (404)
proxy_redirect off;
proxy_set_header Host tt.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://1.1.1.2:8080$request_uri; #原始uri不会镜像,可以通过$request_uri变量取得原始请求的uri
proxy_set_header X-Original-URI $request_uri;
}
#post_action
location @tt_http {
expires off;
proxy_redirect off;
proxy_set_header Host tt.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://1.1.1.2:8080;
}
include deny_file.conf;
include ssl.conf;
access_log /data/logs/$host.log access;
}
2、在1.1.1.2上启动gor 监听8080端口,并将http流量输出到文件
./gor --http-pprof :8080 --input-raw :8080 --input-raw-realip-header "X-Real-IP" --output-file /tmp/goreplay/requests.gor
ps: 输出的文件似乎是被切割的?requests_0.gor、requests_1.gor、requests_2.gor ....
3、利用gor导出的文件,对测试环境进行模拟请求
./gor --input-file /tmp/goreplay/requests_1.gor --output-http="http://t0.example.com"
从测试上看,go_action和mirror的区别在于:
- go_action会等待真实的请求完成,再进行镜像请求,而这个等待过程不影响ngx worker进程接受处理其他的请求
- mirror,向后端发送真实的请求后,不会等待结果而是直接开始镜像请求