Nginx 一个牛X的功能,流量拷贝!

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

1、需求


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

可以验证功能是否正常,以及服务的性能;


用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问;

这跟灰度发布还不太一样,镜像流量不会影响真实流量;


可以用来排查线上问题;


重构,假如服务做了重构,这也是一种测试方式;


为了实现流量拷贝,Nginx提供了ngx_http_mirror_module模块


2、安装Nginx


首页,设置yum仓库。为此,创建一个文件/etc/yum.repos.d/nginx.repo

将以下内容写入文件

微信图片_20220414200506.png


yum安装nginx


yum install nginx


默认情况下,nginx配置文件是nginx.conf


一般情况下,nginx.conf文件在 /usr/local/nginx/conf 或者 /etc/nginx 或者 /usr/local/etc/nginx 目录下


为了启动nginx,直接在命令行里输入nginx回车即可


微信图片_20220414200702.png

微信图片_20220414200719.jpg

微信图片_20220414200731.jpg


一旦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本意是镜子、镜像,这里可以理解就像一个镜像站点一样,将所有的请求都收集起来,这个镜像就代表了所有真实有效的原始请求。有了这个镜像,后续我们才可能用这个镜像去做一些事情,比如重现一下所有的请求,这就实现了把线上的流程复制到别的地方。扩展:SpringBoot 项目构建 Docker 镜像调优实践


官网给出的示例倒是很简单,如下:

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

目录
相关文章
|
4月前
|
负载均衡 网络协议 应用服务中间件
【Nginx】Nginx 功能特性
【1月更文挑战第25天】【Nginx】Nginx 功能特性
|
4月前
|
应用服务中间件 nginx
百度搜索:蓝易云【利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大】
以上就是使用Nginx内置 `ngx_http_mirror_module`模块实现流量复制和流量放大的简要示例。通过合理配置和利用该模块,可以实现更复杂的流量控制和调试需求。
58 1
|
6月前
|
应用服务中间件 nginx
nginx防盗链功能
nginx防盗链功能
|
19天前
|
存储 安全 应用服务中间件
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
解密Nginx限流机制:有效应对DDoS攻击与高并发流量
31 0
|
3月前
|
负载均衡 监控 应用服务中间件
Nginx负载均衡:你的网站流量翻倍利器
Nginx负载均衡:你的网站流量翻倍利器
46 0
|
9月前
|
缓存 负载均衡 网络协议
一文讲懂Nginx常用配置及和基本功能
一文讲懂Nginx常用配置及和基本功能
235 1
|
6月前
|
运维 监控 应用服务中间件
用 Golang 采集 Nginx 接口流量大小
用 Golang 采集 Nginx 接口流量大小
|
6月前
|
运维 应用服务中间件 nginx
运维(27)-部署流量代理(Nginx+haproxy)
运维(27)-部署流量代理(Nginx+haproxy)
64 0
|
7月前
|
缓存 应用服务中间件 网络安全
百度搜索:蓝易云【Nginx常用配置及和基本功能详解!】
这些是Nginx的一些常用配置和基本功能。Nginx还具有更多的高级功能和模块,可以根据具体需求进行灵活配置和扩展。
62 0
|
9月前
|
应用服务中间件 测试技术 nginx
nginx线上流量复制
nginx线上流量复制