当前位置: 代码网 > 服务器>服务器>Linux > Nginx实现接口复制的示例代码

Nginx实现接口复制的示例代码

2025年01月11日 Linux 我要评论
1、前言项目中,通常会遇到一个中转服务需要往多个不同的系统推送同一份数据,传统做法是需要在java代码侧中调用多个api接口进行发送。其实nginx作为一个请求代理转发中间件必然具备类似的功能,常见就

1、前言

项目中,通常会遇到一个中转服务需要往多个不同的系统推送同一份数据,传统做法是需要在java代码侧中调用多个api接口进行发送。其实nginx作为一个请求代理转发中间件必然具备类似的功能,常见就有mirror指令进行流的镜像复制。

2、接口流复制

2.1、方式一:使用mirror指令

注意:要使用nginx的mirror指令,需要nginx安装ngx_http_mirror_module模块。可以通过nginx -v命令查看。nginx 1.13.4及后续版本已经内置了ngx_http_mirror_module模块,之前的版本需要手动编译安装。

示例场景:

  • 发送一个主请求,端口8080;
  • nginx同时转发到8081服务和8082服务;
  • 8081和8082各自access log都有访问记录,且状态正常。

开撸,我这里准备的nginx版本为1.20.1。

2.1.1、nginx配置

server {
  listen 8080;
  index index.php index.html;
  server_name localhost;
  location / {
    mirror /mirror;
    proxy_pass http://localhostserver;
    }
    location = /mirror {
    internal;
    proxy_pass http://mirrorserver$request_uri;
    }
}

upstream localhostserver {
  server localhost:8081;
}

upstream mirrorserver {
  server localhost:8082;
}


server {
  listen 8081;
  server_name localhost;
  access_log /var/log/nginx/8081-access.log;
  root html/local;
}
server {
  listen 8082;
  server_name localhost;
  access_log /var/log/nginx/8082-access.log;
  root html/mirror;
}

接着在nginx的html目录下,创建local目录和mirror目录,并创建文件test.html。test.html内容随便填写:

mkdir -p local  mirror
touch test.html
echo "local.test---> test.html" >> test.html 
echo "mirror.test---> test.html" >> test.html 

2.1.2、配置说明

  • 主请求:/ 会将请求转发到 localhostserver。
  • 镜像请求:配置了 mirror 指令,将请求同时复制一份转发到 /mirror。/mirror 使用了 internal,防止客户端直接访问。
  • 镜像服务:/mirror 会将请求转发到 mirrorserver。

2.1.3、测试结果

由于8081和8082服务都配置了access log,因此当我们访问http://localhost:8080时,预期的结果是访问请求能得到正常的回显,并且8081和8082服务的access log都有相应的访问日志。

8081-access.log:

8082-access.log:

这样就完成了nginx实现接口复制的功能。nginx 实现接口复制的需求通常用于在接收到请求后,将请求数据转发到多个后端服务器(例如用于日志记录、监控或者负载分摊)。

2.1.4、注意事项

  • 镜像请求是非阻塞的,nginx 不会等待镜像请求的响应。
  • 镜像功能仅支持 http 请求,不支持 websocket 或其他非 http 协议。

2.2、方式二:使用lua

nginx支持lua脚本有2种方式:

  • 手动编译nginx,加入ngx_http_lua_module模块;
  • 安装openresty,使用该组件的nginx;

openresty是一个基于 nginx 与 lua 的高性能 web 平台,其内部集成了大量精良的 lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 web 应用、web 服务和动态网关。

2.2.1、安装openresty

下载最新版本的openresty,openresty - 下载。这里下载的是1.27.1.1版本。

这里下的是源码包,需要重新编译。

注:这里直接编译会使用qat硬件加速,你需要手动安装qat_engine依赖。

这里直接禁用硬件加速,编译时排除qat:

./configure --without-http_qat_module
# 安装
make
sudo make install

 # 这里还需要安装resty.http模块
 /usr/local/openresty/bin/opm get ledgetech/lua-resty-http

安装完成后,启动openresty的nginx:

/usr/local/openresty/nginx/sbin/nginx

2.2.2、nginx配置

server {
    listen 8090;
    
        location / {
        content_by_lua_block {
        local http = require "resty.http"
        local res = ngx.location.capture("/localserver")
        
        local httpc = http.new()
        httpc:set_timeout(2000)
        
        local ok, err = httpc:request_uri("http://127.0.0.1:8092/test.html", {
            method = ngx.var.request_method,
            body = ngx.var.request_body,
            headers = ngx.req.get_headers(),
            follow_redirects = true,
        })
        
        if not ok then
            ngx.log(ngx.err, "mirror request failed: ", err)
        end
            ngx.say(res.body)
        }
    }
    
    location /localserver {
        proxy_pass http://127.0.0.1:8091;
    }
    }

    server {
    listen 8091;
    server_name 127.0.0.1;
    access_log /usr/local/openresty/nginx/logs/8091-access.log;
    root html/local;

    }

    server {
    listen 8092;
    server_name 127.0.0.1;
    access_log /usr/local/openresty/nginx/logs/8092-access.log;
    root html/mirror;
    }

2.2.3、配置说明

  • 主请求:/ 会先处理主服务的请求,通过 ngx.location.capture 调用 /localserver。
  • 镜像请求:使用 lua 的 resty.http 库,手动发起 http 请求将数据复制到镜像服务器。
  • 返回响应:将主请求的结果返回给客户端。

2.2.4、测试结果

直接访问请求:curl http://localhost:8090/test.html

8901-access.log:

8902-access.log:

3、小结

  • 实时请求复制:推荐使用 ngx_http_mirror_module,简单易用。
  • 高级控制:如果需要复杂逻辑,使用 ngx_http_lua_module 配合 lua 脚本。
  • 离线分析:通过日志记录请求数据,然后离线处理。

 到此这篇关于nginx实现接口复制的示例代码的文章就介绍到这了,更多相关nginx 接口复制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com