1. 需求

功能需求

在不影响真实业务前提下,支持:

  1. 流量复制,用于线故障分析、系统迁移评估等
  2. 流量放大,通过多倍复制,实现放大流量,用于性能压测

配置需求

  1. 支持或禁止post请求复制
  2. 记录镜像请求的访问日志

2. 解决方案

nginx 1.13.4版本,内置ngx_http_mirror_module模块,能满足上述需求

ngx_http_mirror_module模块特性

  1. 相比tcp-copy的优势:无需录制流量,实时可用,配置相当简单
  2. 源站请求,直接原路返回
  3. 复制请求不影响源站请求,源站nginx-server将流量复制到mirror站后,两者不再有任何交集

3. 配置

下面配置在nginx 1.14.1验证通过,具体配置说明,请看注释信息

3.1 复制请求

server {
listen 80;
server_name web1.www.com;
# 源站配置
location / {
access_log /data/nginx/1.14.1/logs/web1/access.log accesslog;
mirror /mirror;
mirror_request_body on;# Indicates whether the client request body is mirrored. default value is on.
proxy_pass http://web1.upstream.name;
}
# 镜像站点配置
location /mirror {
internal; # 内部配置
proxy_pass http://mirror.web1.upstream.name$request_uri;
proxy_pass_request_body on; # Indicates whether the original request body is passed to the proxied server. default value is on
proxy_set_header X-Original-URI $request_uri; # 使用真实的uri重置uri
}
}

3.2 不允许复制post请求

默认支持post请求,禁止需要将mirror_request_body修改为off,并判断$request_method

server {
listen 80;
server_name web1.www.com; # 源站配置
location / {
access_log /data/nginx/1.14.1/logs/web1/access.log accesslog;
mirror /mirror;
mirror_request_body off;# Indicates whether the client request body is mirrored. default value is on.
proxy_pass http://web1.upstream.name;
} # 镜像站点配置
location /mirror {
# 判断请求方法,不是GET返回403
if ($request_method != GET) {
return 403;
}
internal; # 内部配置
proxy_pass http://mirror.web1.upstream.name$request_uri;
proxy_pass_request_body off; # Indicates whether the original request body is passed to the proxied server. default value is on
proxy_set_header Content-Length ""; # mirror_request_body/proxy_pass_request_body都设置为off,则Conten-length需要设置为"",否则有坑
proxy_set_header X-Original-URI $request_uri; # 使用真实的uri重置uri
}
}

3.3 流量放大

配置多分mirror

server {
listen 80;
server_name web1.www.com;
# 源站配置
location / {
access_log /data/nginx/1.14.1/logs/web1/access.log accesslog;
mirror /mirror;
# 多加一份mirror,流量放大一倍
mirror /mirror;
mirror_request_body on;# Indicates whether the client request body is mirrored. default value is on.
proxy_pass http://web1.upstream.name;
}
# 镜像站点配置
location /mirror {
internal; # 内部配置
proxy_pass http://mirror.web1.upstream.name$request_uri;
proxy_pass_request_body on; # Indicates whether the original request body is passed to the proxied server. default value is on
proxy_set_header X-Original-URI $request_uri; # 使用真实的uri重置uri
}
}

4. mirror日志

mirror中不支持配置access_log,解决方法:mirror-location跳转到server,在server中配置accesslog.

server {
listen 80;
server_name web1.www.com; # 源站配置
location / {
access_log /data/nginx/1.14.1/logs/web1/access.log accesslog;
mirror /mirror;
mirror_request_body off;# Indicates whether the client request body is mirrored. default value is on.
proxy_pass http://web1.upstream.name;
} # 镜像站点配置
location /mirror {
internal; # 内部配置
# 跳转到下面的内部server
proxy_pass http://127.0.0.1:10901$request_uri;
proxy_set_header X-Original-URI $request_uri; # 使用真实的uri重置uri
}
} server {
# server没法设置为内部
listen 127.0.0.1:10901;
location / {
access_log /data/nginx/1.14.1/logs/web1/access.log accesslog;
proxy_pass http://mirror.web1.upstream.name;
}
}

5. 性能评估

  • 测试前提:使用jemeter,在相同环境,使用30个并发,各请求3000次get或post方法,参数一样,一组为有mirror配置,另一组为没mirror配置。
  • 测试结果:mirror性能损失在5%以内,具体如下:

9. 遇到的问题

9.1 镜像配置不正确时,无日志

镜像配置不正确,导致复制操作没正常执行,但是nginx没有响应的错误日志,严重影响调试。非常建议配置镜像日志,配置方法如4. mirror日志

9.2 mirror_request_body/proxy_pass_request_body与Content-Length需配置一致

如果mirror_request_body或者proxy_pass_request_body设置为off,则Content-Length必须设置为"",因为nginx/tomcat处理post请求时,会根据Content-Length获取request_body。如果Content-Length不为空,但是把mirror_request_body、proxy_pass_request_body设置为off,nginx/tomcat以为post有内容,但是实际上request_body没有,nginx会报upstream请求超时,tomcat会报如下错误:

"2018-11-08T17:26:36.803+08:00" "331632b86ec64b829672066a96fc6324"      "department"        "group"   "project_name"        "hostname"    "127.0.0.1"     ""      "/post" "p=11"  "-"     "PostmanRuntime/7.1.1"  "ERROR" "xxx.GlobalControllerAdvice"       "operateExp"    "-"     "26"    "xxxx.GlobalControllerAdvice"       "unknown"       "org.springframework.http.converter.HttpMessageNotReadableException"    "I/O error while reading input message; nested exception is java.net.SocketTimeoutException"    "GlobalControllerAdvice中捕获全局异常"  "org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.net.SocketTimeoutException
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:229)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:158)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

98. 引文

nginx流量复制与放大的更多相关文章

  1. Nginx流量复制

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

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

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

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

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

  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. TCPCopy 线上流量复制工具

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

  8. 使用traefik进行流量复制

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

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

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

  10. Prometheus 监控 Nginx 流量 (三)

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

随机推荐

  1. 从零开始配置 vim(9)——初始配置

    虽然本系列文章叫做从0开始配置vim,似乎我们从一开始就要写vimrc配置文件,但是我们并没有这么做.我们先经过几篇文章了解了下面的几个内容 如何设置vim属性,从而改变vim的特征 配置快捷键,以提 ...

  2. border多层渐变

    .content { margin-top: 19px; border-top: 1px dashed rgba(113, 183, 248, 0.6) !important; border-left ...

  3. 【四】-强化学习入门简介---PaddlePaddlle强化学习及PARL框架

    相关文章: [一]飞桨paddle[GPU.CPU]安装以及环境配置+python入门教学 [二]-Parl基础命令 [三]-Notebook.&pdb.ipdb 调试 [四]-强化学习入门简 ...

  4. C/C++ ShellCode 常用加密方式

    异或加密ShellCode: #include <stdio.h> #include <Windows.h> unsigned char buf[] = "\xba\ ...

  5. 交换变量a,b的值(java)

    方法1:引入中间变量 int a = 10; int b = 20; int temp = a; a = b; b = temp; System.out.println("a = " ...

  6. 如何快速获取AWR中涉及到的表

    最近遇到一个很少见的需求,是关于应用测试方面的. 具体来说,这个应用的测试需求要基于一个固定的时间点数据,而且只能测试一轮,再测试就需要还原到测试前状态. 因为我们使用的存储是分层的(热数据在Flas ...

  7. 【OpenVINO™】在 MacOS 上使用 OpenVINO™ C# API 部署 Yolov5

    在 MacOS 上使用 OpenVINO C# API 部署 Yolov5 项目介绍 YOLOv5 是革命性的 "单阶段"对象检测模型的第五次迭代,旨在实时提供高速.高精度的结果, ...

  8. Linux-CentOS7登录页面出现Hint: caps lock on,输入大小写字母反了(大小写反转问题)

    问题描述:虚拟机CentOS7,输入大小写字母反了,开启capslock的时候变成小写字母了,关闭则变成大写了... 解决办法:只需要执行:setleds +caps 或 setleds -caps  ...

  9. [刺客伍六七&黑客] 魔刀千刃evilblade的使用手册与开源

    0x00 前言 2023.8.15 夜里 非常欢迎使用我的魔刀千刃,并且欢迎各位师傅对我的开源代码进行指导! -–Offense without defense, unparalleled in th ...

  10. NC16666 [NOIP2006]开心的金明

    题目链接 题目 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:"你的房间需要购买哪些物品,怎么布置,你说了算,只 ...