1. 需求

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

  • 可以验证功能是否正常,以及服务的性能;

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

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

  • 可以用来排查线上问题;

  • 重构,假如服务做了重构,这也是一种测试方式;

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

2. 安装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 -y

默认情况下,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;
}

3. 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 http://backend;
} location = /mirror {
internal;
proxy_pass http://test_backend$request_uri;
}

如果请求体被镜像,那么在创建子请求之前会先读取请求体。

location / {
mirror /mirror;
mirror_request_body off;
proxy_pass http://backend;
} location = /mirror {
internal;
proxy_pass http://log_backend;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}

前面我们安装了Nginx,但是里面没有包含我们所需的ngx_http_mirror_module模块,因此,真正要使用的时候最好还是采用自定义安装,即从源码构建。

首先,下载源码  http://nginx.org/en/download.html

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

./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/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=/path/to/ngx_devel_kit
--add-module=/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 http://api.cjs.com;
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 http://tapi.cjs.com$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;
}
}

4. 文档

Nginx文档

http://nginx.org/en/docs/

http://nginx.org/en/docs/http/ngx_http_mirror_module.html

http://nginx.org/en/docs/beginners_guide.html

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

http://nginx.org/en/docs/configure.html

第三方模板 

http://luajit.org/

https://www.nginx.com/resources/wiki/

https://www.nginx.com/resources/wiki/modules/lua/

https://www.nginx.com/resources/wiki/modules/index.html

https://github.com/openresty/lua-nginx-module

补充

# 查看进程运行时间
ps -eo pid,user,lstart,etime,cmd | grep nginx
# 查看已经建立连接的数量
netstat -an | grep ESTABLISHED | wc -l
# 查看80端口的连接数
netstat -an | grep ":80" | wc -l

Nginx流量拷贝的更多相关文章

  1. nginx 流量拷贝模块 ngx_http_mirror_module 安装试用

    1. 下载源码编译 https://nginx.org/download/nginx-1.13.4.tar.gz   2. 下载依赖模块包       这里直接yum 安装 yum -y instal ...

  2. ambassador 学习八 流量拷贝说明

    这个功能nginx 的mirror 插件也支持,基本原理就是数据发送后端,但是不进行响应 参考图 实现方式 原始请求 getambassador.io/config: | --- apiVersion ...

  3. goreplay(gor) golang 流量拷贝工具试用

    1. 项目地址 https://github.com/buger/goreplay 2. 安装 wget https://github.com/buger/goreplay/releases/down ...

  4. Nginx流量复制

    1. 需求 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: 可以验证功能是否正常,以及服务的性能: 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问: 这跟灰度发布还 ...

  5. Nginx 流量和连接数限制

    1.Nginx流量限制 实现流量限制由两个指令 limit_rate 和 limit_rate_after 共同完成: limit_rate 语法:limit_rate rate; 默认值:limit ...

  6. Nginx 流量带宽等请求状态统计( ngx_req_status)

    Nginx 流量带宽等请求状态统计 ( ngx_req_status)  插件下载地址: wget http://nginx.org/download/nginx-1.4.2.tar.gz git c ...

  7. nginx流量带宽等请求状态统计( ngx_req_status)

    介绍 ngx_req_status用来展示nginx请求状态信息,类似于apache的status,nginx自带的模块只能显示连接数等等信息,我们并不能知道到底有哪些请求.以及各url域名所消耗的带 ...

  8. Prometheus 监控 Nginx 流量 (三)

    介绍 基于Openresty和Prometheus.Consul.Grafana设计的,实现了针对域名和Endpoint级别的流量统计,使用Consul做服务发现.KV存储,Grafana做性能图展示 ...

  9. Nginx流量带宽请求状态统计(ngx_req_status)

    介绍           ngx_req_status 用来展示 nginx 请求状态信息,类似于 apache 的 status, nginx 自带的模块只能显示连接数等等 信息,我们并不能知道到底 ...

随机推荐

  1. 【lwip】04-内存管理

    目录 前言 4. 内存管理 4.1 内存分配策略 4.1.1 固定大小的内存块 4.1.2 可变大小分配 4.2 动态内存池(pool) 4.2.1 介绍 4.2.2 内存池的预处理 4.2.3 内存 ...

  2. Notepad++ 过滤注释行和空行

    Notepad++ 删除指定字符开头的行的正则表达式 1.删除A之后的所有字符用:A.*$ 2.删除A之前的所有字符用:^([^s]*)A ####如果是其他字符就把A替换为其他字符 注释:如何是特殊 ...

  3. iNeuOS工业互联网操作系统,发布实时存储方式:实时存储、变化存储、定时存储,增加设备振动状态和电能状态监测驱动,v3.6.2

    目       录 1.      概述... 1 2.      平台演示... 2 3.      存储方式... 2 4.      设备状态和用电状态监控驱动... 3 1.   概述 本次升 ...

  4. MySQL:由于找不到VCRUNTIME140_1.dll,无法继续执行代码。重新安装程序可能会解决此问题

    我只是搬用工,记录一下 方法一: 安装这个微软常用运行库合集(https://www.repaik.com/), 链接:https://pan.baidu.com/s/1r4JJaUKjw-y1g3l ...

  5. 浅谈springboot自动配置原理

    前言 springboot自动配置关键在于@SpringBootApplication注解,启动类之所以作为项目启动的入口,也是因为该注解,下面浅谈下这个注解的作用和实现原理 @SpringBootA ...

  6. Part 1 to 10 Basic in C#

    Part 1 Introduction The struct of C# program: namespace , class and Main method what is namespace? t ...

  7. [年薪60W分水岭]基于Netty手写Apache Dubbo(带注册中心和注解)

    阅读这篇文章之前,建议先阅读和这篇文章关联的内容. 1. 详细剖析分布式微服务架构下网络通信的底层实现原理(图解) 2. (年薪60W的技巧)工作了5年,你真的理解Netty以及为什么要用吗?(深度干 ...

  8. [bzoj1711]吃饭

    由于无法直接将果汁和饮料连边,所以将人放在中间,果汁和饮料放在两侧,然后分别向对应的人连边.同时,为了保证每一个人只被算一次,对每一个人裂点,两个点中间连一条流量为1的边. 1 #include< ...

  9. [bzoj5025]单调上升路径

    由于题目的证明可以发现$ans\ge 2m/n \ge n-1$,于是大胆猜测答案就是n-1若n是奇数,则将边分为n组,每组(n-1)/2,如果同组内边没有交点,那么只需要每一组边一个权值区间,从每一 ...

  10. CKAD认证中的部署教程

    在上一章中,我们已经学会了使用 kubeadm 创建集群和加入新的节点,在本章中,将按照 CKAD 课程的方法重新部署一遍,实际上官方教程的内容不多,笔者写了两篇类似的部署方式,如果已经部署了 kub ...