Nginx以及如何拿到发送给mirror的request和response,并且将request和response打包发送给另一个新的服务?
现在想要在Nginx实现一个流量复制的功能,同时需要将发送给mirror的request和response记录下来,并且将request和response全部再发送给另一个新服务. 应该如何配置Nginx?
配置文件如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$gzip_ratio" $request_time $bytes_sent $request_length';
open_log_file_cache max=1000 inactive=60s;
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
upstream backends {
server localhost:9099;
}
upstream mirror1_backend {
server localhost:9099 weight=10;
server testshadow-shad-36587-default.seldon.svc.cluster.local:8000 weight=90;
}
upstream mirror2_backend {
server localhost:9099 weight=80;
server testshadow-shad-d1c85-default.seldon.svc.cluster.local:8000 weight=20;
}
server {
listen 9099;
location / {
return 200;
}
}
#gzip on;
server {
listen 8000-8050;
listen 9000-9050;
server_name _;
location / {
mirror /mirror1;
mirror /mirror2;
proxy_pass http://backends;
proxy_method $request_method;
mirror_request_body on;
proxy_pass_request_body on;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location = /mirror1 {
internal;
resolver 127.0.0.11 valid=10s;
proxy_pass http://mirror1_backend$request_uri;
proxy_method $request_method;
mirror_request_body on;
proxy_pass_request_body on;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location = /mirror2 {
internal;
resolver 127.0.0.11 valid=10s;
proxy_pass http://mirror2_backend$request_uri;
proxy_method $request_method;
mirror_request_body on;
proxy_pass_request_body on;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
现在已经将请求转发给了mirror1和mirror2,并且分配了比例到相关的服务。
testshadow-shad-36587-default.seldon.svc.cluster.local:8000 和 testshadow-shad-d1c85-default.seldon.svc.cluster.local:8000 是我的测试服务,分别为testS1和testS2。
testS1和testS2接收完全相同的输入,但是输出不一样。
现在的目的是把流量发给testS1和testS2的request和response都记录下来;
我看网上有办法记录到access_log。但是我是要把request和response合计发送给另一个服务,叫做bizlog服务。
现在的问题是,如何收集testS1和testS2的request和response,并且发送给bizlog服务?
使用
mirror
指令将请求复制到/mirror1
和/mirror2
。在
/mirror1
和/mirror2
的位置中,设置internal;
使其仅能被内部调用。使用
proxy_pass
将请求转发到目标服务(如testS1
和testS2
)。使用
log_subrequest on;
启用子请求日志,这样可以记录镜像的请求。将镜像的请求体和响应体发送到
bizlog_service
。确保将Content-Type
设置为JSON
,以便于后端解析。最后,确保
bizlog_service
可以接收并处理来自Nginx
的POST
请求。