Nginx模块-ngx_http_mirror_module-流量复制

参考1:https://www.cnblogs.com/cjsblog/p/12163207.html

Nginx流量复制

1. 需求

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

  • 可以验证功能是否正常,以及服务的性能;
  • 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问;
  • 这跟灰度发布还不太一样,镜像流量不会影响真实流量;
  • 可以用来排查线上问题;
  • 重构,假如服务做了重构,这也是一种测试方式;

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

安装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;
}

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;
    }
}

文档

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

原文地址:https://www.cnblogs.com/passzhang/p/12180862.html

时间: 2024-08-30 08:35:55

Nginx模块-ngx_http_mirror_module-流量复制的相关文章

华为刀片,IBM刀片服务器将外部EXT端口流量复制给内部INT端口的实现方法不同

如何在刀片服务器中将外部端口EXT的流量复制给内部INT端口用于采集DNS流量 (1)对于IBM刀片服务器的北电Nortel_32R1860交换模块112.4.20.12DNS抓包的问题已经搞定 经过摸索和测试,感觉北电的交换机模块不是采用镜像命令来实现复制流量(虽然有相应的port-mirroring monitor-port...命令,但并不生效),而应该是在相同vlan就可以复制流量,如果要把ext3的流量复制给int14,只要把ext3和int14放在同一个vlan11下,都不打tagg

Nginx限制ip链接数,Nginx如何限制并发数,同1个IP,nginx怎么限制流量/限制带宽?

nginx 限制ip并发数,也是说限制同一个ip同时连接服务器的数量.如何Nginx限制同一个ip的连接数,限制并发数目,限制流量/限制带宽? 通过下面nginx模块的使用,我们可以设置一旦并发链接数超过我们的设置,将返回503错误给对方.这样可以非常有效的防止CC攻击.在配合 iptables防火墙,基本上CC攻击就可以无视了.Nginx限制ip链接数,Nginx如何限制并发数,同1个IP,nginx怎么限制流量/限制带宽?请看下文: nginx 限制ip并发数,nginx限制IP链接数的范例

搭建FastDFS分布式存储环境(使用Nginx模块)

上次搭建FastDFS使用的版本是v4.05,见http://www.linuxidc.com/Linux/2014-10/107592.htm这个版本已经比较旧了 最新的版本是v5.04,由于作者重构了代码,所以安装过程还是有一些不一致.最新版本下载地址:http://sourceforge.net/projects/fastdfs/files/安装可以参考压缩包内的INSTALL文件. 实验还是搭建一个FastDFS环境,并增加Nginx模块所用软件:FastDFS_v5.04.tar.gz

FastDFS 配置 Nginx 模块-Linux

1.搭建虚拟机 a.复制虚拟机文件 首先复制我们之前安装好的fastdfs虚拟机,因为我们现在要设置它的IP为21,改名为CentOS-fastdfs - 21. b.设置网络 生成新的MAC地址 设置网络地址为192.168.50.21  # vi /etc/sysconfig/network-scripts/ifcfg-ens33 修改主机名 #vi /etc/sysconfig/network # vi /etc/hosts 重启网络并测试 2.安装配置Nginx模块  a.fastdfs

FastDFS的php和nginx模块配置

一.FastDFS和php整合 1.安装php # 安装依赖包 yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel cu

【重要】Nginx模块之————Lua-Resty-Redis的参数介绍 (Lua-Nginx-Module 模块的Redis客户端驱动程序)

一.描述 这个Lua库是ngx_lua nginx模块的Redis客户端驱动程序:https://github.com/openresty/lua-nginx-module/#readme,这个Lua库利用ngx_lua的cosocket API,确保100%的非阻塞行为.请注意,至少需要ngx_lua 0.5.14或OpenResty 1.2.1.14. 二.方法介绍 除了所有的小写字母外,所有的Redis命令都有自己的方法.您可以在这里找到完整的Redis命令列表:http://redis.

nginx模块开发获取post参数

> 您好!>     我想请问下nginx模块里面怎么获取post参数,能有具体的代码更好!谢谢> 对于 "application/x-www-form-urlencoded" 格式的 POST 参数获取,可以参考 ngx_lua 模块的ngx.req.get_post_args() 函数以及 ngx_form_input 模块的实现: http://wiki.nginx.org/HttpLuaModule#ngx.req.get_post_args https://

nginx 模块nginx_upstream_check_module

我这里用到的nginx为最新版的nginx 所以我使用了最新的插件 nginx_upstream_check_module-master.zip cd nginx-1.7.1 patch -p1 </tmp/nginx_upstream_check_module-master/check_1.5.12+.patch nginx -V ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_s

Nginx模块开发入门(转)

前言 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中,Nginx的占有率为6.8%.与Apache相比,Nginx在高并发情况下具有巨大的性能优势. Nginx属于典型的微内核设计,其内核非常简洁和优雅,同时具有非常高的可扩展性.Nginx最初仅仅主要被用于做反向代理,后来随着HTTP核心的成熟和各种HTTP扩展模块的丰富,Nginx越来越多被用来取代Apache而单独承担HTTP Server的责任,例如目前淘宝内