Nginx實現(xiàn)接口復制的示例代碼
1、前言
項目中,通常會遇到一個中轉(zhuǎn)服務需要往多個不同的系統(tǒng)推送同一份數(shù)據(jù),傳統(tǒng)做法是需要在Java代碼側(cè)中調(diào)用多個API接口進行發(fā)送。其實Nginx作為一個請求代理轉(zhuǎn)發(fā)中間件必然具備類似的功能,常見就有mirror指令進行流的鏡像復制。
2、接口流復制
2.1、方式一:使用mirror指令
注意:要使用nginx的mirror指令,需要nginx安裝ngx_http_mirror_module模塊。可以通過nginx -V命令查看。nginx 1.13.4及后續(xù)版本已經(jīng)內(nèi)置了ngx_http_mirror_module模塊,之前的版本需要手動編譯安裝。
示例場景:
- 發(fā)送一個主請求,端口8080;
- nginx同時轉(zhuǎn)發(fā)到8081服務和8082服務;
- 8081和8082各自access log都有訪問記錄,且狀態(tài)正常。
開擼,我這里準備的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目錄下,創(chuàng)建local目錄和mirror目錄,并創(chuàng)建文件test.html。test.html內(nèi)容隨便填寫:
mkdir -p local mirror touch test.html echo "local.test---> test.html" >> test.html echo "mirror.test---> test.html" >> test.html
2.1.2、配置說明
- 主請求:/ 會將請求轉(zhuǎn)發(fā)到 localhostServer。
- 鏡像請求:配置了 mirror 指令,將請求同時復制一份轉(zhuǎn)發(fā)到 /mirror。/mirror 使用了 internal,防止客戶端直接訪問。
- 鏡像服務:/mirror 會將請求轉(zhuǎn)發(fā)到 mirrorServer。
2.1.3、測試結(jié)果
由于8081和8082服務都配置了access log,因此當我們訪問http://localhost:8080時,預期的結(jié)果是訪問請求能得到正常的回顯,并且8081和8082服務的access log都有相應的訪問日志。

8081-access.log:

8082-access.log:

這樣就完成了nginx實現(xiàn)接口復制的功能。Nginx 實現(xiàn)接口復制的需求通常用于在接收到請求后,將請求數(shù)據(jù)轉(zhuǎn)發(fā)到多個后端服務器(例如用于日志記錄、監(jiān)控或者負載分攤)。
2.1.4、注意事項
- 鏡像請求是非阻塞的,Nginx 不會等待鏡像請求的響應。
- 鏡像功能僅支持 HTTP 請求,不支持 WebSocket 或其他非 HTTP 協(xié)議。
2.2、方式二:使用Lua
Nginx支持Lua腳本有2種方式:
- 手動編譯nginx,加入ngx_http_lua_module模塊;
- 安裝openresty,使用該組件的Nginx;
OpenResty是一個基于 Nginx 與 Lua 的高性能 Web 平臺,其內(nèi)部集成了大量精良的 Lua 庫、第三方模塊以及大多數(shù)的依賴項。用于方便地搭建能夠處理超高并發(fā)、擴展性極高的動態(tài) Web 應用、Web 服務和動態(tài)網(wǎng)關。
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 調(diào)用 /localServer。
- 鏡像請求:使用 Lua 的 resty.http 庫,手動發(fā)起 HTTP 請求將數(shù)據(jù)復制到鏡像服務器。
- 返回響應:將主請求的結(jié)果返回給客戶端。
2.2.4、測試結(jié)果
直接訪問請求:curl http://localhost:8090/test.html
8901-access.log:

8902-access.log:

3、小結(jié)
- 實時請求復制:推薦使用 ngx_http_mirror_module,簡單易用。
- 高級控制:如果需要復雜邏輯,使用 ngx_http_lua_module 配合 Lua 腳本。
- 離線分析:通過日志記錄請求數(shù)據(jù),然后離線處理。
到此這篇關于Nginx實現(xiàn)接口復制的示例代碼的文章就介紹到這了,更多相關Nginx 接口復制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx反向代理多域名的HTTP和HTTPS服務的實現(xiàn)
這篇文章主要介紹了Nginx反向代理多域名的HTTP和HTTPS服務的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
nginx如何使用openssl自簽名實現(xiàn)https登錄
這篇文章主要介紹了nginx使用openssl自簽名實現(xiàn)https登錄,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
Nginx代理MySQL實現(xiàn)通過域名連接數(shù)據(jù)庫的詳細教程
我們的生產(chǎn)環(huán)境基本上都部署在云服務器上,例如應用服務器、MySQL服務器等,如果MySQL服務器直接暴露在公網(wǎng),就會存在很大的風險,為了保證數(shù)據(jù)安全,MySQL服務器的端口是不對外開放的,所以本文介紹了Nginx代理MySQL實現(xiàn)通過域名連接數(shù)據(jù)庫的詳細教程2024-07-07

