二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • 最佳实践

  • 迎刃而解

  • Nginx

    • 关于nginx请求头中有下划线_的坑
    • 利用nginx+sftp实现一个可供用户下载的服务
    • nginx配置文件及模块
    • 通过脚本按天切割nginx的日志
    • nginx通过四层代理实现端口转发
    • NGINX基于cookie针对同一域名进行分流转发
    • nginx利用内置模块配置限速限流
    • 利用NGINX内置模块mirror进行流量复制等操作
      • 1,特性。
      • 2,简单配置。
      • 3,验证。
    • 使用$remote_user字段记录访问NGINX的用户
    • 从NGINX自身配置文件中定义访问日志按时间切割
    • NGINX配置单独代理百度的sitemap文件
    • nginx配置微信小程序校验及其他
    • nginx配置gzip压缩
    • 由Nginx集中代理分散的PHP集群的实践
    • http状态码详解
    • OpenResty-1-13-6-2-新增ldap模块儿
    • 排查NGINX的open_file_cache导致发布后访问404的问题
    • 制作OpenResty-1-19-9-1的RPM包
    • 从Nginx过滤打印user-agent为clb-healthcheck的日志聊聊Nginx的日志自定义打印
  • Php

  • Zabbix

  • AWS

  • Prometheus

  • Grafana

  • Loki

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

  • Etcd

  • Consul

  • RabbitMQ

  • Kafka

  • Mysql

  • MongoDB

  • OpenVPN

  • Kvm

  • VMware

  • 配置文件详解

  • Other

  • 运维观止
  • Nginx
二丫讲梵
2019-10-05
目录

利用NGINX内置模块mirror进行流量复制等操作

文章发布较早,内容可能过时,阅读注意甄别。

在日常工作中,会有这样的场景,为了便于测试,可能希望线上的请求能够同步到测试一部分,以便于验证某些功能,或者是在多套测试环境的情况下,希望能够将某些请求在几个环境同步,比如在 1 环境测试的时候生成了某个图片或者视频,这个生成依赖于一个请求的回调,而如果没有特别配置,则这个请求就只在当前环境中生效,这对测试工作有相当大的不便。

于是,我们需要引入流量复制这一概念,流量复制有不少工具可以实现,有 Gor、tcpreplay、tcpcopy 等,而今天将要使用的,是配置简单,使用方便的 NGINX 的一个模块:ngx_http_mirror_module。

mirror: 中文为镜像的意思,这里指流量复制的目的地。

# 1,特性。

  • nginx 1.13.4 及后续版本内置 ngx_http_mirror_module 模块,提供流量镜像 (复制) 的功能。
  • 支持流量放大,做法为:配置多份相同镜像。
  • 相比 tcp-copy 的优势:无需录制流量,实时可用;配置相当简单。
  • 源站请求,直接原路返回;正常配置下,mirror 请求不影响源站请求及响应,源站 nginx-server 将流量复制到 mirror 站后,两者不再有任何交集。

mirror 模块在 Nginx 1.13.4 以后的版本中默认是启用的,只需看一下版本即可,不必重新编译。

# 2,简单配置。

先看下当前使用的 NGINX 版本:

$ nginx  -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module
1
2
3
4
5
6

接着添加如下配置:

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen 8181;
        access_log /var/log/nginx/test.log;
        root html/test;
    }
    server {
        listen 8282;
        access_log /var/log/nginx/mir1.log;
        root html/mir1;
    }
    server {
        listen 8383;
        access_log /var/log/nginx/mir2.log;
        root html/mir2;
    }
    upstream backend {
        server 127.0.0.1:8181;
    }
    upstream test_backend1 {
        server 127.0.0.1:8282;
    }
    upstream test_backend2 {
        server 127.0.0.1:8383;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            mirror /mirror1;
            mirror /mirror2;
            proxy_pass http://backend;
        }
        location = /mirror1 {
            internal;
            proxy_pass http://test_backend1$request_uri;
        }
        location = /mirror2 {
            internal;
            proxy_pass http://test_backend2$request_uri;
        }
    }
}
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

说明:

location / {                        # location /指定了源uri为/,也可以定义为其他指定接口
    mirror /mirror1;                # mirror /mirror指定镜像uri为/mirror
    mirror /mirror2;                # 有多个需要复制流量的,可以配置多条
    mirror /mirror2;                # 配置多条情况下,将会起到流量放大的作用,即主配置请求一次,镜像端会有两次
  # mirror_request_body on;         # 指定是否镜像请求body部分,请求自动缓存;
    proxy_pass http://backend;      # 指定处理主流量的后端Server
}
location = /mirror1 {
    internal;       # 指定此location只能被“内部的”请求调用,外部的调用请求会返回”Not found” (404)
    proxy_pass http://test_backend1$request_uri;    # 指定将要复制流量的Server1
}
location = /mirror2 {
    internal;
    proxy_pass http://test_backend2$request_uri;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

申明

原创文章eryajf,未经授权,严禁转载,侵权必究!此乃文中随机水印,敬请读者谅解。

Copyright 二丫讲梵 (opens new window) 版权所有

# 3,验证。

此时验证也非常简单,检测配置无误,然后启动 NGINX,使用 tail 同时监听三个日志,然后请求 127.0.0.1/index.html,会发现这一请求同时在日志中出现了。

创建一下访问内容:

$ cd /usr/local/nginx/html/
$ mkdir test mir1 mir2
$ echo test > test/index.html
$ echo mir1 > mir1/index.html
$ echo mir2 > mir2/index.html
$ curl 127.0.0.1/index.html
1
2
3
4
5
6

请求看效果:

img

参考:

  • https://dwz.cn/T1rLisdA
  • https://dwz.cn/8atYbyNk
  • https://dwz.cn/UpMMbdiC
微信 支付宝
#nginx
上次更新: 2024/07/04, 22:40:37
nginx利用内置模块配置限速限流
使用$remote_user字段记录访问NGINX的用户

← nginx利用内置模块配置限速限流 使用$remote_user字段记录访问NGINX的用户→

最近更新
01
记录二五年五一之短暂回归家庭
05-09
02
学习周刊-总第210期-2025年第19周
05-09
03
学习周刊-总第209期-2025年第18周
05-03
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式