Nginx流量复制#

需求#

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

  • 可以验证功能是否正常,以及服务的性能;
  • 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问;
  • 这跟灰度发布还不太一样,镜像流量不会影响真实流量;
  • 可以用来排查线上问题;
  • 重构,假如服务做了重构,这也是一种测试方式;

为了实现流量拷贝,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=
enabled=
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=
enabled=
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  

初始配置文件长这样:

user  nginx;
worker_processes ; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; events {
worker_connections ;
} 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 ; #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 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.
--add-module=/path/to/ngx_devel_kit
--add-module=/path/to/lua-nginx-module make & make install
配置 upstream api.cjs.com {
server 127.0.0.1:;
} upstream tapi.cjs.com {
server 127.0.0.1:;
} server {
listen ;
   # 源站点
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;
}
}

文档#

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模块-ngx_http_mirror_module-流量复制 - PassZhang - 博客园 https://www.cnblogs.com/passzhang/p/12180862.html

参考

利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大 - andersChow的个人空间 - OSCHINA https://my.oschina.net/andChow/blog/2873870

nginx模块ngx_http_mirror_module - 简书 https://www.jianshu.com/p/a0db49d86433

ngx_http_mirror_module (ngx_http_mirror_module) - Nginx 中文开发手册 - 开发者手册 - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/section/1259233

Nginx模块-ngx_http_mirror_module-流量复制【转】的更多相关文章

  1. Nginx模块-ngx_http_mirror_module-流量复制

    参考1:https://www.cnblogs.com/cjsblog/p/12163207.html Nginx流量复制 需求 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: ...

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

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

  3. Nginx流量复制

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

  4. 高并发 Nginx+Lua OpenResty系列(11)——流量复制/AB测试/协程

    流量复制 在实际开发中经常涉及到项目的升级,而该升级不能简单的上线就完事了,需要验证该升级是否兼容老的上线,因此可能需要并行运行两个项目一段时间进行数据比对和校验,待没问题后再进行上线.这其实就需要进 ...

  5. Nginx虚拟主机流量状态模块(nginx-module-vts)使用说明文档(四)

    装完NG,为了拿到各种状态指标,就要对NG做监控. Github 2.3k的开源项目nginx-module-vts没准真是你需求的. 链接数,qps,1xx.2xx,.3xx.4xx.5xx的响应数 ...

  6. nginx 模块及运行机制 第三章

    概述:nginx服务器模块.web请求处理机制及事件驱动模型.进程功能和进程间通信 一:Nginx的模块化结构设计: 1.核心模块:指的是nginx服务器运行当中必不可少的模块,这些模块提供了最基本最 ...

  7. FastDFS 配置 Nginx 模块,并实现分布式同步-Linux

    1.搭建虚拟机 a.复制虚拟机文件 首先复制我们之前安装好的fastdfs虚拟机,因为我们现在要设置它的IP为21,改名为CentOS-fastdfs - 21. b.设置网络 生成新的MAC地址 设 ...

  8. OpenResty / Nginx模块,Lua库和相关资源的列表

    OpenResty / Nginx模块,Lua库和相关资源的列表 什么是OpenResty OpenResty是一个成熟的网络平台,它集成了标准的Nginx核心,LuaJIT,许多精心编写的Lua库, ...

  9. gor实现线上HTTP流量复制压测引流

    一.使用背景 gor 是一款go语言实现的简单的http流量复制工具,它的主要目的是使你的生产环境HTTP真实流量在测试环境和预发布环境重现.只需要在 代理例如nginx入口服务器上执行一个进程,就可 ...

随机推荐

  1. Ajax学习系列——Ajax介绍及优缺点

    一.什么是Ajax Ajax即“Asynchronous JavaScript And XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术. Ajax = 异步Jav ...

  2. 可并堆模板题-mergeable heap

    Description 有n个点,第i个点标号为i,有两种操作:0 x y 表示把x所在堆和y所在堆合并.1 x 表示询问x所在堆的最小权. Input 第一行两个整数n,m,表示有n个点m个操作. ...

  3. 利用标签导出Word文档

    1 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; us ...

  4. c++程序—switch分支

    #include<iostream> using namespace std; #include<string> int main() { //多元分支 cout <&l ...

  5. LeetCode#7 整数反转(数学)

    题目: 思路:(题外话:好久不刷题,明显生疏了好多,要捡起来记住当初那一份热爱!) 判断溢出的方法,在将数字反转的同时,专注在int的最大值/10和最小值/10这两个数上进行判断就可以了: 拿正数为例 ...

  6. 21 ~ express ~ 内容详情展示 和 阅读数处理

    1,前台 ,/views/main/index.html ,将文章 id 通过url 传送给后台 {% for content in contents %} <div class="p ...

  7. java向量 vector

    Vector 向量 是java.util 包里的一个类,该类继承AbstractList,实现了类似动态数组的功能. 向量和数组相似,都可以保存一组数据,但数组的大小(长度)是固定的,而Vector ...

  8. LabVIEW面向对象的ActorFramework(3)

    四.LabVIEW面向对象的编程架构:Actor Framework Actor Framework是一个软件类库,用以支持编写有多个VI独立运行且相互间可通信的应用程序,在该类型应用程序中,每个VI ...

  9. 【分类问题中模型的性能度量(二)】超强整理,超详细解析,一文彻底搞懂ROC、AUC

    文章目录 1.背景 2.ROC曲线 2.1 ROC名称溯源(选看) 2.2 ROC曲线的绘制 3.AUC(Area Under ROC Curve) 3.1 AUC来历 3.2 AUC几何意义 3.3 ...

  10. Linux 压缩解压操作

    Linux 压缩解压操作 Linux解压文件到指定目录 tar在Linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数:-c :create 建立压缩档案的 ...