时间:2022-04-09来源:www.pcxitongcheng.com作者:电脑系统城
Nginx专门提供了ngx_http_mirror_module模块,用来实现流量拷贝。将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处:
ngx_http_mirror_module模块就像是一个镜像站点一样,将所有的请求都收集起来,这个镜像站点就代表了所有真实有效的原始请求。有了这个镜像站点,后续就可以复现所有的请求,实现把线上的流程复制到别的地方。
下面是Nginx解压后,编译安装的示例
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# ./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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
upstream kevin-order { server 127.0.0.1:8088; } upstream kevin-customer { server 127.0.0.1:8089; } upstream kevin-mirror1 { server 172.16.60.230:8088; } upstream kevin-mirror2 { server 172.16.60.230:8089; } server { listen 80; server_name kevin.com; access_log /usr/local/nginx/logs/kevin .com-access.log main; error_log /usr/local/nginx/logs/kevin .com-error.log; # 源站点1 location /order { proxy_pass http: //kevin-order ; 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_request_body on; # 流量复制 mirror /mirror1 ; } # 源站点2 location /customer { proxy_pass http: //kevin-customer ; 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_request_body on; mirror /mirror2 ; } # 镜像站点1 location /mirror1 { proxy_pass http: //kevin-mirror1 $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; } # 镜像站点2 location /mirror2 { proxy_pass http: //kevin-mirror2 $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; } } |
配置说明:上面配置中,将访问http://kevin.com/order、http://kevin.com/customer的流量分别复制到172.16.60.230服务器的8088和8089端口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
server { listen 80; server_name kevin.com; # 源站配置 location / { access_log /usr/local/nginx/logs/access .log accesslog; mirror /mirror ; mirror_request_body on; proxy_pass http: //kevin .upstream.name; } # 镜像站点配置 location /mirror { internal; # 内部配置 proxy_pass http: //mirror .kevin.upstream.name$request_uri; proxy_pass_request_body on; proxy_set_header X-Original-URI $request_uri; #使用真实的url重置url } } |
默认是支持POST流量复制的,需要通过下面配置来禁止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
server { listen 80; server_name kevin.com; # 源站配置 location / { access_log /usr/local/nginx/logs/access .log accesslog; mirror /mirror ; mirror_request_body off; proxy_pass http: //kevin .upstream.name; } # 镜像站点配置 location /mirror { # 判断请求方法,不是GET返回403 if ($request_method != GET) { return 403; } internal; #内部配置 proxy_pass http: //mirror .kevin.upstream.name$request_uri; proxy_pass_request_body off; # mirror_request_body和proxy_pass_request_body都设置为off,则Conten-length需要设置为"",否则有坑! proxy_set_header Content-Length "" ; proxy_set_header X-Original-URI $request_uri; # 使用真实的url重置url } } |
配置多分mirror镜像点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
server { listen 80; server_name kevin.com; # 源站配置 location / { access_log /usr/local/nginx/logs/access .log accesslog; mirror /mirror ; # 多加一份mirror,流量放大一倍 mirror /mirror ; mirror_request_body on; proxy_pass http: //kevin .upstream.name; } # 镜像站点配置 location /mirror { internal; # 内部配置 proxy_pass http: //mirror .kevin.upstream.name$request_uri; proxy_pass_request_body on; proxy_set_header X-Original-URI $request_uri; #使用真实的url重置url } } |
mirror中不支持配置access_log,解决方法:mirror-location跳转到server,在server中配置accesslog。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
server { listen 80; server_name kevin.com; # 源站配置 location / { access_log /usr/local/nginx/logs/access .log accesslog; mirror /mirror ; mirror_request_body on; proxy_pass http: //kevin .upstream.name; } # 镜像站点配置 location /mirror { internal; # 内部配置 # 跳转到下面的内部server proxy_pass http: //127 .0.0.1:10992$request_uri; proxy_pass_request_body off; proxy_set_header Content-Length "" ; proxy_set_header X-Original-URI $request_uri; #使用真实的url重置url } server { # server没法设置为内部 listen 127.0.0.1:10992; location / { # 判断放在server,使得post请求日志可以记录 if ($request_method != GET) { return 403; } access_log /usr/local/nginx/logs/access .log accesslog; proxy_pass http: //mirror .kevin.upstream.name; } } |
镜像配置不正确,导致流量复制操作没正常执行。如果mirror镜像配置缺少日志,会严重影响调试。所以强烈建议配置镜像日志,配置方法如如上"配置mirror镜像日志"。部分错误配置的错误信息在在error日志中。
如果mirror_request_body
或者proxy_pass_request_body
设置为off
,则Content-Length
必须设置为"",因为nginx(mirror_request_body)
或tomcat(mirror_request_body)
处理post请求时,会根据Content-Length
获取请求体,如果Content-Length
不为空,而由于mirror_request_body
或者proxy_pass_request_body
设置为off
,处理方以为post有内容,当request_body
中没有,处理方会一直等待至超时,则前者为off,nginx会报upstream请求超时;后者为off,tomcat会报如下错误:
1 2 3 4 5 6 7 8 9 10 11 |
"2020-11-18T17: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) |
更多关于Nginx流量拷贝技术文章请查看下面的相关链接
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17