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

默认情况下,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+Lua OpenResty系列(11)——流量复制/AB测试/协程

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

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

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

  3. Nginx流量拷贝

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

  4. Nginx 流量和连接数限制

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

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

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

  6. TCPCopy 线上流量复制工具

    TCPCopy是一种重放TCP流的工具,使用真实环境来测试互联网服务器上的应用程序. 一.描述: 虽然真实的实时流量对于Internet服务器应用程序的测试很重要,但是由于生产环境中的情况很负责,测试 ...

  7. 使用traefik进行流量复制

    文章转载自:https://mp.weixin.qq.com/s/nMMN7hAJK6SFn1V1YyxvHA Traefik 2.0 还引入了镜像服务,一种可以将流入流量复制并同时将其发送给其他服务 ...

  8. nginx mirror/post_action+gor实现https流量复制

    关于gor: 参考: https://www.cnblogs.com/jinjiangongzuoshi/p/11773070.html https://github.com/buger/gorepl ...

  9. Prometheus 监控 Nginx 流量 (三)

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

随机推荐

  1. visual studio 2013 修改mvc5的视图模板

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffol ...

  2. SuperSocket特点

    ²  简单易用,只需要几个类就能创建出健壮的Socket服务器端程序 ²  性能优良, 稳定可靠 ²  支持各种协议, 内置的协议解析工具让你把实现通信协议这种复杂的工作变得很简单 ²  自动支持SS ...

  3. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. NGINX比Apache的性能高是因为NGINX由C语言开发,而Apache由C++开发

    事实上,NGINX比Apache的性能高是因为NGINX由C语言开发,而Apache由C++开发.因此,NGINX效率大概是Apache的10倍左右

  5. poj 2993

    跟poj 2996反过来了,这里比较麻烦的就是处理白棋和黑棋各棋子对应的位置 还有在最后打印棋盘式|,:,.的时候会有点繁琐(- - ACMer新手 ): 直接看代码吧: #include<cs ...

  6. 日历价差(calendar spread)

    日历价差(calendar spread) 是指投资者买进到期日较远的期权 (简称远期期权),同时又卖出相同行权价格.相同数量但到期日较近的期权(简称近期期权),赚取两个不同期权隐含波动率的差价或者其 ...

  7. mybatis查询无结果, 数据库运行相同sql查询出结果

    一.问题描述 mybatis查询无结果, 数据库运行相同sql查询出结果, 如下 这是数据库记录 这是mybatis查询出的结果, 记录条数0 这是直接将控制台一模一样的sql查询语句放到Navica ...

  8. uni-app 常用框架内置方法 更新中 .....

    获取 登录信息,getStorage 初始化页面数据   请求  下拉刷新页面 加载更多  点击跳转  个人中心  uni.request(OBJECT)   success=成功    fail=失 ...

  9. Python--day34--socket模块的方法

    官方文档对socket模块下的socket.send()和socket.sendall()的解释如下: sk.setblocking(False)方法 import socket sk = socke ...

  10. 9月29更新美版T-mobile版本iPhone7代和7P有锁机卡贴解锁方法

    ​ T版是块难解的砖头,之前一直没有找到稳定解锁办法,经过多次不写努力和实验,终于解决 不管是用超雪卡贴还是GPP卡贴,第一次先用连接WIFI激活手机! 注意:一定不要用ICCID通用激活,或者是TM ...