Azure Functions Proxy的backendUri是否可引用request.headers值
Azure Functions Proxies 配置问题解答
结论
你尝试的在backendUri字段直接拼接request.headers变量的用法不被官方支持。
官方已支持的变量规则
目前Azure Functions Proxies仅支持两类变量的替换逻辑:
backendUri字段仅支持使用%变量名%格式读取AppSettings(应用配置)中的值,符合规范的示例如下:
{ "$schema": "http://json.schemastore.org/proxies", "proxies": { "myProxyName": { "matchCondition": { "route": "/" }, "backendUri": "%MY_APP_SETTING%", "requestOverrides": { "backend.request.headers.myCustomHeader":"request.headers.host" } } } }
requestOverrides字段支持读取请求上下文变量(包括request.headers、请求路由参数、查询参数等),可用于修改转发到后端的请求头、查询参数等属性。
你提供的示例配置无法生效的原因
backendUri的动态替换逻辑目前没有开放请求上下文类变量的读取权限,仅能识别应用配置占位符,因此下面的写法不符合官方规范:
{ "$schema": "http://json.schemastore.org/proxies", "proxies": { "myProxyName": { "matchCondition": { "route": "/" }, "backendUri": "%MY_APP_SETTING%/{request.headers.my_header_value}" } } }
替代实现方案
如果你需要把请求头的取值传递到后端,可以选择两种可行方案:
- 借助
requestOverrides把请求头的值转为后端可读取的查询参数,示例配置:
{ "$schema": "http://json.schemastore.org/proxies", "proxies": { "myProxyName": { "matchCondition": { "route": "/" }, "backendUri": "%MY_APP_SETTING%", "requestOverrides": { "backend.request.querystring.header_val": "request.headers.my_header_value" } } } }
后端直接读取header_val查询参数即可获取对应请求头的值。
2. 如果必须把值放到URI路径中,可以调整匹配路由,把需要的参数通过路由占位符传入,backendUri支持直接引用路由占位符,示例:
{ "$schema": "http://json.schemastore.org/proxies", "proxies": { "myProxyName": { "matchCondition": { "route": "/{path_param}" }, "backendUri": "%MY_APP_SETTING%/{path_param}" } } }
在调用代理的时候把原本要放到请求头里的值作为路由路径的一部分传入即可。
内容的提问来源于stack exchange,提问作者Florian Eckert
相关产品推荐
相关产品推荐

