nginx实时流量拷贝ngx_http_mirror_module

参考:
Module ngx_http_mirror_module
Nginx流量拷贝ngx_http_mirror_module模块使用方法详解

ngx_http_mirror_module用于实时流量拷贝

请求一个接口,想实时拷贝这个请求转发到自己的服务上,可以使用ngx_http_mirror_module模块。

官网好像就两个指令,

bash 复制代码
mirror
mirror_request_body

例子

案例流程:

curl client -> 本地nginx -> 测试服务(这里用的是一个nginx模拟的服务)

bash 复制代码
~/data/nginx/web/conf/ cat default.conf 

upstream kevin-mirror2 {
    server 192.168.52.133:80;   # 这个实际上是另一个nginx
}
server {

    listen       80;
    listen  [::]:80;
    server_name  localhost;

    access_log  /var/log/nginx/host.access.log  main;

    location ~* \.(jpg|JPG|PNG|jpeg|png|gif)$ {
        root /static;
    }

    location / {
#        root   /usr/share/nginx/html;
#        index  index.html index.htm;
        mirror /mirror;
        proxy_pass http://kevin-mirror2;
    }

    location /mc {
        set            $memcached_key "$uri";
        memcached_pass 192.168.59.213:11211;
    }

    location = /mirror {
        internal;
        access_log /usr/share/nginx/html/mirror.log  main;
        proxy_pass http://kevin-mirror2$request_uri;   # 这里这里要写成http://kevin-mirror2$request_uri,写成http://kevin-mirror2转发到后端就变成了/mirror 
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI $request_uri;
        proxy_set_header X-Real-IP $remote_addr;
    }


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

(p38)  ~/data/nginx/web/conf/ 
  1. 请求镜像端点,返回404

    本地nginx显示404
  2. 访问/本地nginx返回页面
  • 本地nginx返回页面
  • 本地nginxnginx的日志
  • 代理的测试服务。因为是测试,我把请求都转到了测试nginx上,这里可以看到的确有两个请求过来。
相关推荐
励志不掉头发的内向程序员5 分钟前
【Linux系列】掌控 Linux 的脉搏:深入理解进程控制
linux·运维·服务器·开发语言·学习
東雪蓮☆9 分钟前
K8s Ingress 详解与部署实战
linux·运维·kubernetes
塔能物联运维15 分钟前
物联网运维中的边缘计算任务调度优化策略
运维·人工智能·物联网·边缘计算
真正的醒悟26 分钟前
什么是网络割接
运维·服务器·网络
Bruce_Liuxiaowei31 分钟前
Win7虚拟机加入域错误排查指南:解决无法启动服务问题
运维·网络·windows·安全·网络安全
聆风吟º1 小时前
无需 VNC / 公网 IP!用 Docker-Webtop+cpolar,在手机浏览器远程操控 Linux
linux·运维·docker
骇客野人1 小时前
【软考备考】 高并发场景如何做负载均衡知识点四
运维·负载均衡
序属秋秋秋3 小时前
《Linux系统编程之入门基础》【Linux基础 理论+命令】(上)
linux·运维·服务器·ubuntu·centos·命令模式
知白守黑2675 小时前
docker资源限制
运维·docker·容器
霍格沃兹测试开发学社测试人社区5 小时前
新手指南:通过 Playwright MCP Server 为 AI Agent 实现浏览器自动化能力
运维·人工智能·自动化