当前位置:首页 > Web开发 > 正文

镜像流量不会影响真实流量; 可以用来排查线上问题; 重构

2024-03-31 Web开发

参考1:https://www.cnblogs.com/cjsblog/p/12163207.html

Nginx流量复制 1. 需求

将出产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,,好比:

可以验证成果是否正常,以及处事的性能;

用真实有效的流量请求去验证,又不用造数据,不影响线上正常访谒;

这跟灰度颁布还不太一样,镜像流量不会影响真实流量;

可以用来排查线上问题;

重构,假如处事做了重构,这也是一种测试方法;

为了实现流量拷贝,Nginx供给了ngx_http_mirror_module模块

安置Nginx

首页,设置yum货仓。为此,创建一个文件/etc/yum.repos.d/nginx.repo
将以下内容写入文件

[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true Yum安置nginx yum install nginx 默认情况下,nginx配置文件是nginx.conf 一般情况下,nginx.conf文件在 /usr/local/nginx/conf 或者 /etc/nginx 或者 /usr/local/etc/nginx 目录下 为了启动nginx,直接在命令行里输入nginx回车即可 # 启动nginx nginx # fast shutdown nginx -s stop # graceful shutdown nginx -s quit # reloading the configuration file nginx -s reload # reopening the log files nginx -s reopen # list of all running nginx processes ps -ax | grep nginx

一旦master进程接收到从头加载配置的信号,它将查抄新配置文件的语法是否正确,并测验考试应用此中供给的配置。如果告成,master进程将启动新的worker进程,并发送动静给旧的worker进程,要求他们shutdown。否则,master进程将回滚所做的变动,并继续使用旧配置。旧的worker进程在接收到封锁命令后,遏制接受新的连接,直到所有之前已经接受的连接全部措置惩罚惩罚完为止。之后,旧的worker进程退出。

nginx的master进程的进程ID,默认情况下,放在nginx.pid文件中,该文件地址的目录一般是/usr/local/nginx/logs 或者 /var/run

还可以这样遏制nginx

kill -s QUIT 3997

初始配置文件长这样:

user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; 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"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } ngx_http_mirror_module

The ngx_http_mirror_module module (1.13.4) implements mirroring of an original request by creating background mirror subrequests. Responses to mirror subrequests are ignored.
我是这样理解的,这里,mirror本意是镜子、镜像,这里可以理解就像一个镜像站点一样,将所有的请求都收集起来,这个镜像就代表了所有真实有效的原始请求。有了这个镜像,后续我们才可能用这个镜像去做一些工作,好比重现一下所有的请求,这就实现了把线上的流程复制到另外处所。

官网给出的示例却是很简单,如下: location / { mirror /mirror; proxy_pass ; } location = /mirror { internal; proxy_pass $request_uri; } 如果请求体被镜像,那么在创建子请求之前会先读取请求体 location / { mirror /mirror; mirror_request_body off; proxy_pass ; } location = /mirror { internal; proxy_pass ; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header X-Original-URI $request_uri; }

前面我们安置了Nginx,但是里面没有包罗我们所需的ngx_http_mirror_module模块,因此,真正要使用的时候最好还是给与自界说安置,即从源码构建
首先,下载源码

接下来,编译安置,例如:

./configure --sbin-path=http://www.mamicode.com/usr/local/nginx/nginx --conf-path=http://www.mamicode.com/usr/local/nginx/nginx.conf --pid-path=http://www.mamicode.com/usr/local/nginx/nginx.pid --with-http_ssl_module --without-http_limit_req_module --without-http_mirror_module --with-pcre=../pcre-8.43 --with-zlib=../zlib-1.2.11 --add-module=http://www.mamicode.com/path/to/ngx_devel_kit --add-module=http://www.mamicode.com/path/to/lua-nginx-module make & make install 配置 upstream api.abc.com { server 127.0.0.1:8080; } upstream tapi.abc.com { server 127.0.0.1:8081; } server { listen 80;    # 源站点 location /api { proxy_pass ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 流量复制 mirror /newapi; mirror /mirror2; mirror /mirror3; # 复制请求体 mirror_request_body on; } # 镜像站点 location /tapi { proxy_pass $request_uri; proxy_pass_request_body on; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } 文档 Nginx文档 #location

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31780.html