大网数据广告活动
服务器运维问答

NGINX - OpenResty - How to reverse proxy a call to 2 different servers based based on a string?

· 52CE 我爱测

I'm trying to configure NGINX/OpenResty to proxy SOAP calls to 2 different servers based on a string present on the SOAP request. What I'm able to do: I am able to proxy requests to 2 different servers based on the path the SOAP …

问题原文

原帖链接 · jedelvalle · CC BY-SA 4.0

I'm trying to configure NGINX/OpenResty to proxy SOAP calls to 2 different servers based on a string present on the SOAP request.

What I'm able to do: I am able to proxy requests to 2 different servers based on the path the SOAP client is calling:

location /pathA {
    proxy_pass http://www.ServerA.com/PathA/;
}
location /pathB {
    proxy_pass http://www.ServerB.com/PathB/;
}

What I can't do:

I can't separate the traffic based on the content of the request. The main reason I believe is that I can't correctly assemble the LUA script to extract the information and later use it to proxy the request.

location / {
    conten_by_lua '
        ngx.req.read_body()
        local match = ngx.re.match(ngx.var.request_body,"STRING TO FIND")
        if match then
            proxy_pass http://www.ServerA.com/PathA/;
        else
            proxy_pass http://www.ServerB.com/PathB/;

how can I achieve this?

I installed OpenResty and LUA is working fine.

I think I read somewhere that if the request is not an HTTP POST ngx.req.read_body() would not work. Is that correct?

Thank you for your help.

社区高分解答

原帖链接 · Ivan Shatsky · CC BY-SA 4.0

You almost did it youself, the only thing I would do differently is to use rewrite_by_lua instead of content_by_lua:

location / {
    set $proxy "";
    rewrite_by_lua '
        ngx.req.read_body()
        local match = ngx.re.match(ngx.var.request_body, "STRING TO FIND")
        if match then
            ngx.var.proxy = "www.ServerA.com"
        else
            ngx.var.proxy = "www.ServerB.com"
        end
    ';
    proxy_pass http://$proxy$uri;
}

If request is not an HTTP POST or has an empty body, ngx.req.read_body() will return nil, so it is better to add an additional check:

location / {
    set $proxy "";
    rewrite_by_lua '
        ngx.req.read_body()
        local body = ngx.var.request_body
        if (body) then
            local match = ngx.re.match(body, "STRING TO FIND")
            if match then
                ngx.var.proxy = "www.ServerA.com"
            else
                ngx.var.proxy = "www.ServerB.com"
            end
        else
            ngx.status = ngx.HTTP_NOT_FOUND
            ngx.exit(ngx.status)
        end
    ';
    proxy_pass http://$proxy$uri;
}

You also can limit allowed methods:

location / {
    limit_except POST { deny all; }
    ...
}

One more thing. With this configuration, if you specify your backends with domain names instead of IP addresses, you'll need a resolver directive in your config. You can use your local name server if you have one, or use something external like Google public DNS (8.8.8.8) or DNS provided for you by your ISP.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-12-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。