nginx根据报文里字段转发至不同地址

nginx接收到post请求.请求报文里是一个json字符串,字符串里有个字段id。

根据id不同,转发到不同地址。

如果id=aaa,转发到www.aaa.com.test

如果id=bbb,转发到www.bbb.com.test

如何配置,请提供一个nginx.conf

要在 Nginx 中根据 POST 请求的 JSON 负载中的 id 字段的值进行转发,你可以使用 Nginx 的 ngx_http_lua_module 模块,这个模块允许你在 Nginx 配置中使用 Lua 脚本。下面是一个示例配置,它展示了如何根据 POST 请求的 JSON 负载中的 id 字段的值来转发请求。

首先,确保你的 Nginx 已经安装了 lua-nginx-modulelua-resty-http 库。这些可以通过 OpenResty 来安装,OpenResty 是一个集成了常用模块的 Nginx 发行版。

安装 OpenResty

你可以通过以下命令安装 OpenResty(假设你使用的是基于 Debian 的系统,如 Ubuntu):

bash 复制代码
sudo apt-get update
sudo apt-get install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl -O https://openresty.org/package/pubkey.gpg
sudo apt-key add pubkey.gpg
sudo apt-get install -y apt-transport-https
echo "deb https://openresty.org/package/debian $(lsb_release -sc) openresty" | sudo tee /etc/apt/sources.list.d/openresty.list
sudo apt-get update
sudo apt-get install -y openresty

Nginx 配置 (nginx.conf)

下面是一个示例的 nginx.conf 配置,它使用 Lua 脚本来解析 POST 请求的 JSON 负载,并根据 id 字段的值转发请求。

nginx 复制代码
http {
    lua_package_path "/usr/local/openresty/luajit/share/lua/5.1/?.lua;;";
    lua_shared_dict my_cache 10m;

    server {
        listen 80;

        location / {
            content_by_lua_block {
                -- 读取 POST 请求体
                ngx.req.read_body()
                local data = ngx.req.get_body_data()

                if not data then
                    ngx.status = ngx.HTTP_BAD_REQUEST
                    ngx.say("No request body found")
                    return ngx.exit(ngx.HTTP_BAD_REQUEST)
                end

                -- 解析 JSON
                local cjson = require "cjson.safe"
                local json_data = cjson.decode(data)

                if not json_data or not json_data.id then
                    ngx.status = ngx.HTTP_BAD_REQUEST
                    ngx.say("Invalid JSON or missing 'id' field")
                    return ngx.exit(ngx.HTTP_BAD_REQUEST)
                end

                local id = json_data.id
                local target_url

                if id == "aaa" then
                    target_url = "http://www.aaa.com.test"
                elseif id == "bbb" then
                    target_url = "http://www.bbb.com.test"
                else
                    ngx.status = ngx.HTTP_NOT_FOUND
                    ngx.say("Unknown 'id' value")
                    return ngx.exit(ngx.HTTP_NOT_FOUND)
                end

                -- 创建 HTTP 客户端并转发请求
                local http = require "resty.http"
                local httpc = http.new()
                local res, err = httpc:request_uri(target_url, {
                    method = ngx.var.request_method,
                    body = data,
                    headers = ngx.req.get_headers(),
                    keepalive = false,
                })

                if not res then
                    ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR
                    ngx.say("Failed to forward request: ", err)
                    return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
                end

                -- 设置响应头
                for k, v in pairs(res.headers) do
                    ngx.header[k] = v
                end

                -- 发送响应体
                ngx.status = res.status
                ngx.print(res.body)

                -- 关闭 HTTP 客户端
                httpc:close()
            }
        }
    }
}

解释

  1. lua_package_pathlua_shared_dict:设置 Lua 模块的搜索路径和共享字典。
  2. serverlocation:定义一个服务器块和一个位置块来处理进入的请求。
  3. content_by_lua_block:使用 Lua 脚本处理请求。
  4. ngx.req.read_body():读取 POST 请求体。
  5. cjson.decode(data):解析 JSON 数据。
  6. 条件判断 :根据 id 字段的值决定目标 URL。
  7. resty.http :使用 resty.http 模块创建 HTTP 客户端并转发请求。
  8. 设置响应头和状态:将目标服务器的响应头和状态码传递给客户端。
  9. 发送响应体:将目标服务器的响应体发送给客户端。

确保你安装了 lua-cjsonlua-resty-http 库,这通常在使用 OpenResty 时已经包含在内。如果你手动安装 Nginx 和 Lua 模块,则需要确保这些库可用。

重启 Nginx

在修改完 nginx.conf 后,不要忘记重启 Nginx 以应用新的配置:

bash 复制代码
sudo systemctl restart nginx

这样,Nginx 将能够根据 POST 请求的 JSON 负载中的 id 字段的值来转发请求。

相关推荐
冬奇Lab2 小时前
一天一个开源项目(第46篇):Caddy - 自动 HTTPS 的现代化 Web 服务器,支持 HTTP/3
网络协议·nginx·开源
顺风尿一寸2 天前
Nginx源码分析:变量系统的设计与请求生命周期中的日志记录
nginx
金銀銅鐵2 天前
浅解 JUnit 4 第十五篇:如何在测试方法运行前后做些事情?
junit·单元测试
爱吃橘子橙子柚子2 天前
3CPU性能排查总结(超详细)【Linux性能优化】
运维·cpu
金銀銅鐵3 天前
浅解 JUnit 4 第十四篇:如何实现一个 @After 注解的替代品?
junit·单元测试
金銀銅鐵3 天前
浅解 JUnit 4 第十三篇:如何实现一个 @Before 注解的替代品?(下)
junit·单元测试
舒一笑4 天前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
NineData4 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库
梦想很大很大5 天前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair5 天前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维