我正在设置一个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
。
我尝试添加请求覆盖以添加额外的标头,如下所示:
"requestOverrides": {
"backend.request.headers.X-Original-URL": "{request.originalUrl}"
}
但是,这不起作用,并且X-Original-Url
标头包含\n\u000fx-forwarded-for\u0012\texample.com
值
如何可靠地获取功能应用程序收到的原始URL?
答案 0 :(得分:0)
事实证明,originalUrl
的原始意图正是这样,以保留用户调用的原始URL。如果我们真正处理没有任何MS代理但可以重写的node.js应用程序,则它将是这样的。
因此,在我的情况下,我必须使用requestOverrides
和自定义标头来提供所需的信息。我的proxies.json
现在有这个:
在我的函数中,我可以通过以下方式重构网址:
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;
似乎没有办法获得原始协议(http vs https),但是就我而言,这并不重要,因为无论如何我都在使用https。