如何在Envoy配置中访问属性并将request.path传入YAML配置
解决方案
要将Envoy的request.path属性值作为请求头传递,你需要使用Envoy的属性模板语法,而非直接写入静态字符串——当前配置里的request.path会被当作普通文本发送,不会解析为实际的请求路径。
修改后的关键配置片段
针对/service/1的路由,调整request_headers_to_add部分:
routes: - match: prefix: "/service/1" request_headers_to_add: - header: key: "test-path" value: "{{ request.path }}" header_value_format: TEXT_FORMAT route: cluster: service1
完整配置示例
- filters: - name: envoy.filters.network.http_connection_manager typed_config: "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager codec_type: AUTO stat_prefix: ingress_http route_config: name: local_route virtual_hosts: - name: backend domains: - "*" routes: - match: prefix: "/service/1" request_headers_to_add: - header: key: "test-path" value: "{{ request.path }}" header_value_format: TEXT_FORMAT route: cluster: service1 - match: prefix: "/service/2" route: cluster: service2 http_filters: - name: envoy.filters.http.router typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
说明
header_value_format: TEXT_FORMAT:告知Envoy需要解析value字段中的模板占位符,而非当作静态文本处理。{{ request.path }}:引用Envoy内置的请求路径属性,会被替换为当前请求的实际路径值。- 若需要传递未经过路由修改的原始请求路径,可替换为
{{ request.original_path }}。
内容的提问来源于stack exchange,提问作者Shawon0418
相关产品推荐
相关产品推荐

