Nginx proxy_pass 到一个变量 url

Nginx proxy_pass to a variable url

我是 Nginx 新手。据我所知,我们可以在 nginx.conf 中配置 proxy_pass url,例如:

location /test
{
     proxy_pass http://10.10.10.10:10000;
}

但是现在,我需要proxy_pass一个变量url,我们可以在http请求header或请求body中得到这个url(例如)。

我该如何实现?感谢您的帮助!

编辑 1(解释我的问题):

有一个http请求"redirectUrl"在它的header发送给Nginx,我只是想让Nginx修改这个http请求header然后发送这个请求到重定向网址。因为 redirectUrl 是一个变量,所以我猜 "proxy_pass $request" 可以实现这个,但我不知道如何实现。谁能给我提示或有任何文档吗?

编辑2:

我试过了 ngx_lua :

location /apiproxytest {

    set_by_lua $redirectURL '
            return ngx.req.get_headers()["Host"]
    ';

    header_filter_by_lua '
             ngx.header["RedirectURL"] = nil;
             ngx.header["Host"] = nil;
    ';

    echo "1:" $redirectURL;

    set_by_lua $redirectURL2 '
            return ngx.req.get_headers()["Host"]
    ';

    echo "2:" $redirectURL2;

    #proxy_pass $redirectURL;
}   

我发现 echo 1 和 echo 2 是一样的,正如 ngx.header.HEADER 解释的那样,ngx.header["Host"] = nil 应该删除 http 请求中的 Host header.为什么echo "2:" $redirectURL2也可以打印Host的值?

尝试ngx_lua

ngx.req.get_headersset_by_lua 中获取 redirectUrl,然后设置为类似 $redirectURL 的东西,最后 proxy_pass $redirectURL

为编辑 2 添加:

Why echo 1 and echo 2 were the same?

这是Nginx造成的Phases

setset_by_lua工作在rewrite阶段,echo工作在content阶段,header_filter_by_lua工作在output-header-filter阶段(doc),也就是后面的内容。

您的配置是这样工作的:

set_by_lua -> set_by_lua -> echo 1 -> echo 2 -> header_filter_by_lua.