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  ;
        # 源站配置
        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://;
        }
        # 镜像站点配置
        location /mirror {
                internal; # 内部配置
                proxy_pass http://mirror.$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  ;

        # 源站配置
        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://;
        }

        # 镜像站点配置
        location /mirror {
                # 判断请求方法,不是GET返回403
                if ($request_method != GET) {
                    return 403;
                }
                internal; # 内部配置
                proxy_pass http://mirror.$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  ;
        # 源站配置
        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://;
        }
        # 镜像站点配置
        location /mirror {
                internal; # 内部配置
                proxy_pass http://mirror.$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  ;

        # 源站配置
        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://;
        }

        # 镜像站点配置
        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.;
    }
}

5. 性能评估

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

nginx 流复制 nginx 复制请求_java

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 .SocketTimeoutException"    "GlobalControllerAdvice中捕获全局异常"  "org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is .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)