gpt4 book ai didi

azure - 在带有代理的Azure函数中,获取原始URL

转载 作者:行者123 更新时间:2023-12-03 04:15:11 25 4
gpt4 key购买 nike

我正在设置一个 Azure 函数应用,其中将对子域的所有请求代理到一个特定函数。我的 proxies.json 如下所示:

{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"Root URI to Trigger Function": {
"matchCondition": {
"route": "/{*path}",
"methods": [
"GET",
"POST"
]
},
"backendUri": "http://example.com/api/myfunc"
}
}
}

在我的 myfunc 函数中,我尝试使用 req.originalUrl 访问原始请求的 URL - 但它始终设置为 http:///example.com/api/myfunc

我尝试添加请求覆盖以添加额外的 header ,如下所示:

"requestOverrides": {
"backend.request.headers.X-Original-URL": "{request.originalUrl}"
}

但是,这不起作用,X-Original-Url header 包含 \n\u000fx-forwarded-for\u0012\texample.com

如何可靠地获取函数应用收到的原始 URL?

最佳答案

事实证明,originalUrl 的初衷正是如此,保留用户调用的原始 URL。如果我们真的处理一个没有任何 MS 代理但可以重写的 Node.js 应用程序,这就是它的工作方式。

因此,就我而言,我必须使用 requestOverrides 和自定义 header 来满足我的需要。我的 proxies.json 现在有这个:

在我的函数中,我可以这样重建 URL:

let origHost = req.headers.hasOwnProperty('x-original-host') ? req.headers['x-original-host'] : req.headers['host'];
let origPath = req.headers.hasOwnProperty('x-original-path') ? req.headers['x-original-path'] : ''
let search = new URL(req.url).search;

let originalUrl = 'https://' + origHost + '/' + origPath + (search ? '?' : '') + search;

似乎没有办法获取原始协议(protocol)(http 与 https),但就我而言,这并不重要,因为无论如何我都在使用 https。

关于azure - 在带有代理的Azure函数中,获取原始URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53632511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com