You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Envoy配置实现指定URL到localhost的反向代理?

问题描述

需要用Envoy做反向代理,把http://example.com:3443/node-exporter/metrics的请求转发到http://localhost:9100/metrics,也就是将带/node-exporter/前缀的请求路径转换为目标服务的/metrics路径。

当前的envoy_conf.yaml配置如下:

listeners:
 - name: prom_listener
   address:
     socket_address : {address: 0.0.0.0, port_value: 3443}
   filter_chains:
   - name: prom_filter_chain
     filters:
     - name: envoy.filters.network.http_connection_manager
       typed_config:
         "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
         stat_prefix: http_connection_manager
         route_config:
           virtual_hosts:
           - name: prom_local_host
             domains: ["*"]
             routes:
             - name: node-exporter-route
               match: {prefix: "/node-exporter/"}
               route:
                 cluster: node-exporter-cluster-server
                 timeout: 0s
                 idle_timeout: 0s
         http_filters:
         - name: envoy.filters.http.router
           typed_config:
             "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
 - name: node-exporter-cluster-server
   type: static
   connect_timeout: 2s
   load_assignment:
     cluster_name: node-exporter-cluster-server
     endpoints:
     - lb_endpoints:
       - endpoint:
           address:
             socket_address:
               address: 127.0.0.1
               port_value: 9100
修改方案

核心是添加路径重写规则,具体修改如下:

  • 在路由的route节点下添加prefix_rewrite: "/":Envoy会把请求中/node-exporter/的前缀替换为/,这样/node-exporter/metrics就会被转发到目标服务的/metrics。
  • (可选)如果只想精确匹配/node-exporter/metrics这一个路径,可将match的prefix改为exact: "/node-exporter/metrics",同时把prefix_rewrite设为"/metrics",避免其他带/node-exporter/前缀的请求被误转发。

修改后的完整配置

listeners:
 - name: prom_listener
   address:
     socket_address : {address: 0.0.0.0, port_value: 3443}
   filter_chains:
   - name: prom_filter_chain
     filters:
     - name: envoy.filters.network.http_connection_manager
       typed_config:
         "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
         stat_prefix: http_connection_manager
         route_config:
           virtual_hosts:
           - name: prom_local_host
             domains: ["*"]
             routes:
             - name: node-exporter-route
               match: {prefix: "/node-exporter/"}
               route:
                 cluster: node-exporter-cluster-server
                 timeout: 0s
                 idle_timeout: 0s
                 prefix_rewrite: "/"  # 新增路径重写规则
         http_filters:
         - name: envoy.filters.http.router
           typed_config:
             "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
 - name: node-exporter-cluster-server
   type: static
   connect_timeout: 2s
   load_assignment:
     cluster_name: node-exporter-cluster-server
     endpoints:
     - lb_endpoints:
       - endpoint:
           address:
             socket_address:
               address: 127.0.0.1
               port_value: 9100

精确匹配版本(可选)

如果仅需转发指定路径,可调整路由匹配规则:

routes:
- name: node-exporter-route
  match: {exact: "/node-exporter/metrics"}
  route:
    cluster: node-exporter-cluster-server
    timeout: 0s
    idle_timeout: 0s
    prefix_rewrite: "/metrics"

内容的提问来源于stack exchange,提问作者rylp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:45:38